Sultan Kenjeyev Sultan Kenjeyev - 3 months ago 10
HTTP Question

Getting an error issuing a post request in c#

I want to issue a post request in a c# app to https://www.inventor-s-hub.xyz:8000/v8
but I keep getting this error --> System.Net.WebException: the remote name couldn't be resolved 'www.inventor-s-hub.xyz'
I have a node.js server running on the port at that domain which works fine if you navigate to it from a browser.
This is the method that I am calling later on in the app which issues a post request:

Using System.Net.Http;
public async void PostToServer(string name)
{
using (HttpClient client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{ "name", name},
{ "id", "1" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("https://www.inventor-s-hub.xyz:8000/v8", content);

}
}


I've searched the web but couldn't really find anything relevant, I don't think I am running a proxy on the machine I am issuing the request from.
From the server, I am just sending a 200 status.

Answer

Try this sample:

public static async PostToServer(string name)
{
    var values = new Dictionary<string, string>
                                {
                                   { "name", name},
                                   { "id", "1" }
                                };

    HttpClient client = new HttpClient();
    var content = new FormUrlEncodedContent(values);

    HttpResponseMessage response = await client.PostAsync(new Uri("https://www.inventor-s-hub.xyz:8000/v8"), content );

    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();

    return responseBody;
}