C#无法将POST数据写入https请求 [英] c# can't write POST data to https request

查看:134
本文介绍了C#无法将POST数据写入https请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题绝对是奇怪的,但我感谢任何建议. 应用程序使用3个线程工作,并发送POST https请求.我使用内部代理(Fiddler2)时,所有3个请求均已成功发送.当我不使用内部代理时,将发送2个请求,并且最后一个请求(取决于更快地到达请求的线程)失败,并带有超时"异常.它只是无法写入POST数据.这是请求的示例(在单独的线程中发送了3次):

Issue is absolutely strange, but I appreciate any suggestions. Application works using 3 threads and sends POST https requests. The thing that when I'm using internal proxy (Fiddler2) then all 3 requests are sent successfully. When I'm not using internal proxy then 2 requests are sent and the last request (depending on what thread is got to the request faster) is failed with "Timeout" exception. It just can't write POST data. Here is the example of request (which is sent 3 times in separate threads):

 //some data before request
 byte[] byteArray3 = Encoding.ASCII.GetBytes(post_data_final3);
 HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(site_URI);
 WebHeaderCollection NewHeaders3 = request3.Headers;
 request3.CookieContainer = new CookieContainer();
 request3.Method = "POST";
 request3.Timeout = 60000;
 //some headers info here
 try
 {
     using (Stream os3 = request3.GetRequestStream())
     {
          os3.Write(byteArray3, 0, byteArray3.Length);
     }
 }
 catch (WebException ex33)
 {
     Console.WriteLine(ex33);
     Console.WriteLine(ex33.Status);
 }
 try
 {
     HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse();
     //response handling..
 }
 catch (Exception e) {//catching exception}

在情况1(使用Fiddler2)中,上述所有3个请求均运行良好. 在第2种情况(不使用本地代理)的情况下,有2项请求做得很好,最新的一个请求在舞台上引发了异常:

In case 1 (with using Fiddler2) all 3 requests as example above worked well. In case 2 (without using local proxy) 2 requests done well and the latest one throws exception on stage:

 try
 {
      using (Stream os3 = request3.GetRequestStream())
      {
           os3.Write(byteArray3, 0, byteArray3.Length);
      }
 }
 catch (WebException ex33)
 {
      Console.WriteLine(ex33);
      Console.WriteLine(ex33.Status);
 }

请注意,多线程和锁定已实现,因此请在Proxy和/或https协议中的某个地方使用. 我每次都可以使用本地代理,但我不想这样做.谢谢!

Note that multithreading and locking is implemented OK, so issue somewhere in Proxy and/or https protocol using. I can use local proxy every time, but I don't want to. Thanks!

已更新:找到解决方案.在阶段结束时关闭所有响应即可:

UPDATED: solution found. Closing all responses on end stage becomes working:

try
{
     HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse();
     //response handling..
}
catch (Exception e) {//catching exception}
finally { response3.Close(); }

推荐答案

请参阅 HTTPS Web请求失败

从历史上看,我所见过的大多数描述问题都在发生 当您忘记在从返回的对象上调用.Close()时 GetResponseStream().存在该问题是因为当您忘记 关闭第一个请求,第二个请求死锁等待 免费连接.

Historically, most problems of this description that I've seen occur when you forget to call .Close() on the object returned from GetResponseStream(). The problem exists because when you forget to close the first request, the second request deadlocks waiting for a free connection.

通常,此挂起发生在第三个请求上,而不是第二个.

Typically this hang happens on the 3rd request, not the second.

更新:查看您的复制品,这与订单无关 的请求.您遇到问题了,因为此网站正在发送 HTTPS握手开始时出现TLS警告,.NET将 发生这种情况时超时.看

Update: Looking at your repro, this has nothing to do with the order of the requests. You're hitting a problem because this site is sending a TLS Warning at the beginning of the HTTPS handshake, and .NET will timeout when that occurs. See http://blogs.msdn.com/b/fiddler/archive/2012/03/29/https-request-hangs-.net-application-connection-on-tls-server-name-indicator-warning.aspx. The problem only repros on Windows Vista and later, because the warning is related to a TLS extension that doesn't exist in the HTTPS stack on WinXP.

但是Fiddler2如何处理并使其起作用对我来说仍然是不可思议的事情.

But the thing how Fiddler2 handles this and make this works is still a magic to me.

这篇关于C#无法将POST数据写入https请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆