2016-08-19 67 views
0

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

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状态。

+0

但你可以执行后?我的意思是,你是否正确地配置了/ v8路径来接收邮件,例如使用邮递员? – Forlani

+0

@Forlani是的,我已经测试邮递员的路线,它工作正常,所以错误必须在其他地方,但谢谢你的建议。实际上为我工作的是.EnsureSuccessStatusCode并将帖子正文的格式从FormUrlEncoded更改为json,这是我认为问题在第一位置 –

回答

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