Anish Kurian
Anish Kurian

Reputation: 93

Request timed out while using Post method HttClient in C#

In my application, I am using a code piece to send a content to a web proxy with a URL with an interval of 2 seconds between each post. The message to post in the below code is given as static string, but will be dynamic in the original application code.

for (ic = 0; ic < 30; ic++)
{
    using(HttpClient httpClient = new HttpClient())
    {
        Encoder encoder = new Encoder(); //This is a class which we created
        AuthorizationHeaders authorizationHeaders = encoder.getAuthorization(PARTNER_ID, PARTNER_KEY, URL, "POST", "application/xml");

        httpClient.DefaultHeaders.Add("Accept", "*/*");
        httpClient.DefaultHeaders.Add("Authorization", authorizationHeaders.getAuthorizationHeader());
        httpClient.DefaultHeaders.Add("Expires", authorizationHeaders.getExpiresHeader());
        httpClient.DefaultHeaders.Add("origPartnerId", "MyWebSite");
        httpClient.DefaultHeaders.Add("appId", "440");
        httpClient.DefaultHeaders.Add("Content-Type", "application/xml");
        string inputUrl = ConfigurationManager.AppSettings["url"];
        String detailRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Message><Details><Name>Bob</Name><Age>45</Age></Details></Message>";
        var content = HttpContent.Create(detailRequest, Encoding.UTF8, "application/xml");
        response = httpClient.Post(inputUrl, content).EnsureStatusIsSuccessful();
        Thread.Sleep(2000);
    }
}

But I am getting the below exception when contacting the proxy. I am not getting it all the time, the message does go through some time, but eventually, this exception comes.

GetResponse timed out ---> System.TimeoutException: GetResponse timed out ---> System.Net.WebException ---> System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse() at Microsoft.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponseSync(WebRequest request, IAsyncResult result) at Microsoft.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponse(HttpTransportAsyncResult self, IAsyncResult result, Func`3 getResponse) --- End of inner exception stack trace --- --- End of inner exception stack trace --- at Microsoft.Http.AsyncResult.End[TAsyncResult](IAsyncResult result, Boolean throwException) at Microsoft.Http.HttpWebRequestTransportStage.EndProcessRequestAndTryGetResponse(IAsyncResult result, HttpResponseMessage& response, Object& state) at Microsoft.Http.HttpWebRequestTransportStage.ProcessRequestAndTryGetResponse(HttpRequestMessage request, HttpResponseMessage& response, Object& state) at Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self) --- End of inner exception stack trace --- at Microsoft.Http.HttpStageProcessingAsyncResult.Complete(HttpStage stage, Exception e) at Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self) at Microsoft.Http.HttpClient.Send(HttpRequestMessage request) at Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri, RequestHeaders headers, HttpContent content) at Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, Uri uri, HttpContent body) at Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, String uri, HttpContent body)

But this proxy is just a redirection to another one. So I tried directly to that one also. There, I was able to do this 2 or 3 times successfully but it generated another exception after that. I tried again, but this time, the exception came for the first try itself. I waited for some time and then tried again, and surprisingly, it did go through 2 or 3 times and then again the exception came. The exception that I am getting this time is different and it is this...

Microsoft.Http.HttpStageProcessingException ---> System.Net.WebException ---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly. at System.Net.HttpWebRequest.GetResponse() at Microsoft.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponseSync(WebRequest request, IAsyncResult result) at Microsoft.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponse(HttpTransportAsyncResult self, IAsyncResult result, Func3 getResponse) --- End of inner exception stack trace --- at Microsoft.Http.AsyncResult.End[TAsyncResult](IAsyncResult result, Boolean throwException) at Microsoft.Http.AsyncResult.End[TAsyncResult](IAsyncResult result) at Microsoft.Http.AsyncResult1.End(IAsyncResult result) at Microsoft.Http.HttpWebRequestTransportStage.EndProcessRequestAndTryGetResponse(IAsyncResult result, HttpResponseMessage& response, Object& state) at Microsoft.Http.HttpWebRequestTransportStage.ProcessRequestAndTryGetResponse(HttpRequestMessage request, HttpResponseMessage& response, Object& state) at Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self) --- End of inner exception stack trace --- at Microsoft.Http.HttpStageProcessingAsyncResult.Complete(HttpStage stage, Exception e) at Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self) at Microsoft.Http.HttpStageProcessingAsyncResult..ctor(HttpStageProcessingAsyncState state, AsyncCallback callback, Object user) at Microsoft.Http.HttpClient.Send(HttpRequestMessage request) at Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri, RequestHeaders headers, HttpContent content) at Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri, HttpContent content) at Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, Uri uri, HttpContent body) at Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, String uri, HttpContent body) at Microsoft.Http.HttpMethodExtensions.Post(HttpClient client, String uri, HttpContent body)

Any help in this regard would be greatly appreciated because I am after this for the past few days and I have read some articles and even questions in stackoverflow also, but nothing helped me. Thanks in advance...

Upvotes: 0

Views: 1101

Answers (1)

Aidan Host
Aidan Host

Reputation: 146

The proxy rules of some companies will cause problems with POST requests.

GET requests work fine. Check that your OS proxy setting is disabled.

Windows 7: Control Panel > Internet Options > Connections (Tab).

Then click on the "LAN settings" button and then uncheck "Use a proxy server for your LAN".

Alternatively, try and get your Network Admins to add the connection that you are using to the exclusion list.

Upvotes: 0

Related Questions