2

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

Philo
  • 1,931
  • 12
  • 39
  • 77
  • Additional comments:- 1) I turned SSL off and successfully connected to the web api on port 8080. 2) I successfully connected to other SSL api's with my code. Seems like the C# code doesn't connect to ncrApi on SSL connection..any ideas why? – Philo Jul 25 '16 at 17:16

2 Answers2

1

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

Community
  • 1
  • 1
Henrique Forlani
  • 1,325
  • 9
  • 22
  • thanks. I tried that and same error. However this time I turned off SSL. and just connected to localhost:8080/ncrapi and that worked. I tried connecting to other SSL api's and my program successfully connects with them – Philo Jul 25 '16 at 17:15
  • and have you correctly configured your ncrapi endpoint to accept incoming SSL connections on port 8443? It could be some server side issue related to your endpoint. – Henrique Forlani Jul 25 '16 at 17:23
  • I have just found this, take a look: http://stackoverflow.com/questions/22251689/make-https-call-using-httpclient It can also be that you don't have a trusted certificate ... – Henrique Forlani Jul 25 '16 at 17:30
  • thanks!! that solved my issue. I am getting a 200 OK response now. But no data. lol the conundrums continue – Philo Jul 25 '16 at 18:00
  • alright! can you mark it as answer please? I will edit with the reference – Henrique Forlani Jul 25 '16 at 18:05
0

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.