在c#中发出post请求时出错

时间:2016-08-19 17:44:53

标签: c# .net http post asynchronous

我想在c#app中向https://www.inventor-s-hub.xyz:8000/v8发出一个帖子请求 但我一直收到这个错误 - > System.Net.WebException:远程名称无法解析' www.inventor-s-hub.xyz' 我有一个node.js服务器在该域的端口上运行,如果从浏览器导航到它,它可以正常工作。 这是我稍后在发布帖子请求的应用程序中调用的方法:

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);

      }
}

我在网上搜索过但无法找到任何相关内容,我不认为我在发出请求的机器上运行代理。 从服务器,我只是发送200状态。

1 个答案:

答案 0 :(得分:0)

试试这个样本:

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;
}
相关问题