0
votes

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...

1
A comment to add:- It posts for exactly 2 times. Not 2 or 3 times. Sorry for not being specific.Anish Kurian
Is this proxy/or remote machine in your control? In that case did you check what the value for connection timeout interval is set on that machine? This does not look like a problem in your code (other than the fact that you should handle the error and retry)Niyaz
But we still believe that the problem is with our code... Means, there should be something which we miss - which connects to the resource utilization or so. I am saying this because, this works well every time we restart the application pool. If it times out again and we restart the application pool, it works. This compelled us to believe that there should be something with this HttpClient which we are missing. It does not have any connection with the time out value of the server. This is because,from the server log,we could find that the request(which got timed out)has not reached the server.Anish Kurian

1 Answers

0
votes

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.