简体   繁体   中英

C# POST request to webapi using a console program

I have a web api that I can access successfully through a browser :-

https://127.0.0.1:8443/ncrApi

I am trying to create a simple console program in C# using VS2015 to send data and receive a response using http POST.

Here is what I have so far:-

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace WebSample
{

    class ApiSendData
    {
        public string ApiFunction { get; set; }
        public string DppName { get; set; }
        public string ClearData { get; set; }
        public string DppVersion { get; set; }
    }



    class Program
    {
        static void Main(string[] args)
        {
            // The Main function calls an async method named RunAsync 
            // and then blocks until RunAsyncc completes.
            RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            using (var client = new HttpClient())
            {
                // This code sets the base URI for HTTP requests, 
                // and sets the Accept header to "application/json", 
                // which tells the server to send data in JSON format.
                client.BaseAddress = new Uri("https://localhost:8443/ncrApi");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                // HTTP POST
                var datatobeSent = new ApiSendData()
                                    {
                                        ApiFunction ="NcrSecureData",
                                        DppName ="CSampleCustomer",
                                        DppVersion ="Latest",
                                        ClearData ="1234567890"
                                    };

                HttpResponseMessage response = await client.PostAsJsonAsync("ncrApi", datatobeSent);

                if (response.IsSuccessStatusCode)
                {
                    // Get the URI of the created resource.
                    Uri ncrUrl = response.Headers.Location;

                    // do whatever you need to do here with the returned data //


                }
            }
        }
    }
}

However I am getting an error on the HttpResonseMessage response statement....{"An error occurred while sending the request."} {"The request was aborted: Could not create SSL/TLS secure channel."}

I suspect it is because I am not correctly understanding the the client.BaseAddress and the HttpResponseMessage response statements.

Here is what I have been following:- http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

You are probably getting an error because the final address is your baseAddress + post address, that is: http://localhost:8443/nrcApi/nrcApi , which doesn't exist

Try changing your client.BaseAddress to:

client.BaseAddress = new Uri("https://localhost:8443/");

For SSL connection errors, try generating a trusted certificate: Make https call using httpclient

Your code looks ok. However, the issue seems to be the call you are making. Basically in the following call, the first parameter is the method/function to be invoked after your URI.

HttpResponseMessage response = await client.PostAsJsonAsync("ncrApi", datatobeSent);

In this case ncrApi is your method. Calling it in the base URL will result in it being added to the final call making it give an address to an endpoint that does not exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM