2017-03-17 71 views
1

我在使用http客户端使用Accept,ContentType和Authentication作为标题进行POST时遇到问题。我尝试了几个使用send aysnc和post async的实现,并且都失败了。我使用https://www.hurl.it/来确定它是否也出现超时错误,但它正在工作,找回json数据。超时错误:HttpClient POST

运行下面的代码时,出现连接超时错误异常(在:var response = await client.SendAsync(request);)。

此外,我有curl命令,我试图去掉,这也列在下面。

实现:

// Body 
      var dictionary = new Dictionary<string, string>(); 
      dictionary.Add("id", "123"); 

      var formData = new List<KeyValuePair<string, string>>(); 
      formData.Add(new KeyValuePair<string, string>("id", "123")); 

      // Http Client 
      var client = new HttpClient(); 
      client.BaseAddress = new Uri("https://www.some_website.com/api/house"); 

      // Http Request 
      var request = new HttpRequestMessage(HttpMethod.Post, "https://www.some_website.com/api/house"); 

      // Accept 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

      // Content-Type 
      //request.Content = new FormUrlEncodedContent(dictionary); 

      var httpContent = new StringContent(
        JsonConvert.SerializeObject(
         dictionary, 
         new JsonSerializerSettings 
         { 
          NullValueHandling = NullValueHandling.Ignore 
         }), 
        Encoding.UTF8, 
        "application/json"); 

      request.Content = new StringContent(
        JsonConvert.SerializeObject(
         dictionary, 
         new JsonSerializerSettings 
         { 
          NullValueHandling = NullValueHandling.Ignore 
         }), 
        Encoding.UTF8, 
        "application/json"); 

      // Authentication 
      var byteArray = new UTF8Encoding().GetBytes("user" + ":" + "pass"); 
      //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 

      var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 
      client.DefaultRequestHeaders.Authorization = header; 

      // Communication 
      try 
      { 
       var response = await client.SendAsync(request);//PostAsync("https://www.some_website.com/api/house", httpContent); 
       if (response.IsSuccessStatusCode) 
       { 
        var myContent = await response.Content.ReadAsStringAsync(); 
        var deliveryStatus = JsonConvert.DeserializeObject<MyOjbect>(myContent); 
       } 
      } 
      catch (Exception e) 
      { 
       //catch error 
      } 

卷曲:

curl -i --user user:123 -H Accept:application/json -H content-type:application/json -H cache-control:no-cache -X POST https://www.some_website.com/api/house -H Content-Type: application/json -d '{"id": "123"}' 
+1

您将BaseAddress和HttpRequestMessage Uri属性设置为相同绝对url,不应该是基地址(例如。 https://somewebsite.com)和相关的网址(例如(api/house)?您还可以使用Fiddler检查您的请求http://www.telerik.com/fiddler –

+1

我认为您是正确的。改变,仍然有连接超时错误 – PLOW

+1

问题已经解决!代码是正确的。问题是,我使用校园的无线网络,所以我在一个代理服务器。回家,使用我的wifi给我没有时间错误 – PLOW

回答

2

您设定BaseAddress和HttpRequestMessage URI属性都相同的绝对URL,应该不会是基地址(例如somewebsite您可以使用Fiddler来检查您的请求telerik.com/fiddler