超时错误:HttpClient POST

时间:2017-03-17 21:03:40

标签: c# rest http-headers httpclient http-authentication

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

运行以下代码时出现连接超时错误异常(在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 个答案:

答案 0 :(得分:2)

您将BaseAddress和HttpRequestMessage Uri属性设置为相同的绝对URL,不应该是基地址(例如somewebsite.com)和相对URL(例如(api / house)。)您还可以使用Fiddler telerik.com/fiddler

检查您的请求