Search code examples
c#resthttp-headershttpclienthttp-authentication

Time out error: HttpClient POST


I'm having trouble making POST using http client with Accept, ContentType and Authentication as headers. I tried several implementations using send aysnc and post async and all seemed to fail. I used https://www.hurl.it/ to make sure if it also was getting a time out error but it is working, getting back json data.

I get a connection time out error exception (in: var response = await client.SendAsync(request);) when running the below code.

Also I have curl command which i was trying to go off by, which is listed below as well.

Implementation:

// 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:

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"}'

Solution

  • You set your BaseAddress and the HttpRequestMessage Uri property both to the same absolute url, shouldn't it be the base address (ex. somewebsite.com) and the relative url (ex. (api/house). You can also inspect your request with Fiddler telerik.com/fiddler