在我的应用程序中,我使用一段代码将内容发送到一个带有 URL 的 Web 代理,每个帖子之间的间隔为 2 秒。在以下代码中发布的消息以静态字符串的形式给出,但在原始应用程序代码中将是动态的。
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);
}
}
但是我在联系代理时遇到了以下异常。我不是一直都明白,消息确实经过了一段时间,但最终,这个异常来了。
GetResponse 超时 ---> System.TimeoutException: GetResponse 超时 ---> System.Net.WebException ---> System.Net.WebException: 该操作在 Microsoft 的 System.Net.HttpWebRequest.GetResponse() 处超时.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponseSync(WebRequest request, IAsyncResult result) at Microsoft.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponse(HttpTransportAsyncResult self, IAsyncResult result, Func`3 getResponse) --- 内部异常堆栈跟踪结束 --- - -- 内部异常堆栈跟踪结束 --- 在 Microsoft.Http.AsyncResult.End[TAsyncResult](IAsyncResult result, Boolean throwException) 在 Microsoft.Http.HttpWebRequestTransportStage.EndProcessRequestAndTryGetResponse(IAsyncResult 结果, HttpResponseMessage&response, Object& state) 在 Microsoft.Http.Http.HttpWebRequestTransportStage.ProcessRequestAndTryGetResponse(HttpRequestMessage request, HttpResponseMessage& response, Object& state) 在 Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self) --- End of internal exception stack trace--- 在 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) 在 Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, Uri uri, HttpContent body) 在 Microsoft。Http.HttpMethodExtensions.Method(HttpClient 客户端,HttpMethod 方法,String uri,HttpContent 正文)
但是这个代理只是重定向到另一个代理。所以我也直接尝试了那个。在那里,我能够成功地执行此操作 2 或 3 次,但之后又产生了另一个异常。我又试了一次,但这一次,第一次尝试本身就出现了异常。我等了一段时间,然后再次尝试,令人惊讶的是,它确实经历了 2 或 3 次,然后异常再次出现。这次我得到的例外是不同的,就是这个......
Microsoft.Http.HttpStageProcessingException ---> System.Net.WebException ---> System.Net.WebException:底层连接已关闭:连接意外关闭。在 System.Net.HttpWebRequest.GetResponse() 在 Microsoft.Http.HttpWebRequestTransportStage.HttpTransportAsyncResult.PopulateWebResponseSync(WebRequest request, IAsyncResult result) 在 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.AsyncResult
1.在 Microsoft.Http.HttpWebRequestTransportStage.ProcessRequestAndTryGetResponse(HttpRequestMessage request, HttpResponseMessage& response, Object& state) 在 Microsoft.Http.HttpStageProcessingAsyncResult 的 Microsoft.Http.HttpWebRequestTransportStage.EndProcessRequestAndTryGetResponse(IAsyncResult 结果, HttpResponseMessage& 响应, Object& 状态) 的结束(IAsyncResult 结果)。 NextRequest(HttpStageProcessingAsyncResult self) --- 内部异常堆栈跟踪结束 --- Microsoft.Http.HttpStageProcessingAsyncResult.Complete(HttpStage stage, Exception e) 在 Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self) 在 Microsoft.Http.HttpStageProcessingAsyncResult ..ctor(HttpStageProcessingAsyncState 状态,AsyncCallback 回调,对象用户)在 Microsoft.Http.HttpClient。在 Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri, HttpContent content) 在 Microsoft.Http.HttpMethodExtensions 在 Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri, HttpContent content) 在 Microsoft.Http.HttpMethodExtensions 发送(HttpRequestMessage 请求) .Method(HttpClient 客户端, HttpMethod 方法, Uri uri, HttpContent body) 在 Microsoft.Http.HttpMethodExtensions.Method(HttpClient 客户端, HttpMethod 方法, String uri, HttpContent body) 在 Microsoft.Http.HttpMethodExtensions.Post(HttpClient 客户端, String uri , HttpContent 正文)Microsoft.Http.HttpMethodExtensions.Post(HttpClient 客户端,字符串 uri,HttpContent 正文)上的 Http.HttpMethodExtensions.Method(HttpClient 客户端,HttpMethod 方法,字符串 uri,HttpContent 正文)Microsoft.Http.HttpMethodExtensions.Post(HttpClient 客户端,字符串 uri,HttpContent 正文)上的 Http.HttpMethodExtensions.Method(HttpClient 客户端,HttpMethod 方法,字符串 uri,HttpContent 正文)
在这方面的任何帮助将不胜感激,因为过去几天我一直在关注此问题,并且我也阅读了一些文章,甚至还阅读了 stackoverflow 中的一些问题,但没有任何帮助。提前致谢...