GetRequestStream() 返回 WebException 超时...但仅在某些机器上

Posted

技术标签:

【中文标题】GetRequestStream() 返回 WebException 超时...但仅在某些机器上【英文标题】:GetRequestStream() returning WebException time out ... but only on some machines 【发布时间】:2014-01-08 16:44:18 【问题描述】:

在第一次“使用”时抛出异常: using (var os = firstRequest.GetRequestStream())

我现在的猜测是它与服务器发起的 ssl 信任问题有关,但如果这是真的,那么 4 台 windows 7 pro 机器之间有什么不同,其中 2 台工作正常,2 台抛出异常,

异常详情:

System.Net.WebException was unhandled
  Message=The operation has timed out
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
       at System.Net.HttpWebRequest.GetRequestStream()
       at MyCheckAPI.MyCheckLogic2.HttpPost4(String URI, String URI2, String Parameters1, String Parameters2) in C:\Users\Ha-Erez\Desktop\RichTB\RichTB\MyCheckLogic2.cs:line 309
       at MyCheckAPI.MyCheckLogic2.MobileGetCode2(String email, String pass) in C:\Users\Ha-Erez\Desktop\RichTB\RichTB\MyCheckLogic2.cs:line 444
       at RichTB.Integration.button5_Click(Object sender, EventArgs e) in C:\Users\Ha-Erez\Desktop\RichTB\RichTB\Form1.cs:line 348
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at RichTB.Program.Main() in C:\Users\Ha-Erez\Desktop\RichTB\RichTB\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

会是什么?

public static string HttpPost4(string URI, string URI2, string Parameters1, string Parameters2)  

       string respStr = String.Empty;
       CookieContainer cookieJar = new CookieContainer();
       HttpWebRequest firstRequest = (HttpWebRequest)WebRequest.Create(URI);
       try
       
            firstRequest.CookieContainer = cookieJar;
            firstRequest.KeepAlive = true;
            firstRequest.ContentType = "application/x-www-form-urlencoded";
            firstRequest.Method = "POST";
            firstRequest.KeepAlive = false;
            firstRequest.Timeout = 5000;
            firstRequest.Proxy = null;
            firstRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
            firstRequest.ServicePoint.MaxIdleTime = 5000;

             byte[] bytes = System.Text.Encoding.UTF8.GetBytes(Parameters1);
             firstRequest.ContentLength = bytes.Length;
             using (var os = firstRequest.GetRequestStream())
                  
                 os.Write(bytes, 0, bytes.Length);
                os.Close();
             

             using (HttpWebResponse firstResponse = (HttpWebResponse)firstRequest.GetResponse())
                     
                         using (var sr = new StreamReader(firstResponse.GetResponseStream()))
                         

                             respStr = sr.ReadToEnd().Trim();
                             Logger(DateTime.Now + " Login Response: " + respStr, 1);

                             sr.Close();
                         
                         firstResponse.Close();
                     

                     //  cookieJar.Add(firstResponse.Cookies);

                     HttpWebRequest secondRequest = (HttpWebRequest)WebRequest.Create(URI2);
                     secondRequest.Method = "POST";
                     secondRequest.ContentType = "application/x-www-form-urlencoded";
                     // secondRequest.KeepAlive = true;

                     secondRequest.KeepAlive = false;
                     secondRequest.Timeout = 5000;

                     secondRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
                     secondRequest.ServicePoint.MaxIdleTime = 5000;

                     // secondRequest.Headers["Set-Cookie"] = firstResponse.Headers["Set-Cookie"];
                     secondRequest.CookieContainer = cookieJar;
                     byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(Parameters2);
                     secondRequest.ContentLength = bytes2.Length;
                     //   httpWebRequest.
                     using (var os2 = secondRequest.GetRequestStream())
                     
                         os2.Write(bytes2, 0, bytes2.Length);
                         os2.Close();
                     
                     using (WebResponse secondResponse = secondRequest.GetResponse())
                     
                         using (var sr = new StreamReader(secondResponse.GetResponseStream()))
                         

                             respStr = sr.ReadToEnd().Trim();
                             sr.Close();
                         
                     

                     return respStr;
                 
                 catch (Exception ee)
                 
                     Logger(DateTime.Now + " , "+ ee.Message +" , "+ ee.StackTrace, 1);
                 
                 finally
                 
                     firstRequest.Abort();
                 
                return respStr;
             

【问题讨论】:

异常详情是什么? iv'e 重新编辑了问题:添加了异常详细信息 【参考方案1】:

这是SSL 证书问题,服务器处理不当。 当重定向到不同的服务器时,一切都适用于所有场景。 还通过将 .net 4 升级到 .net 4.5 在客户端解决

【讨论】:

以上是关于GetRequestStream() 返回 WebException 超时...但仅在某些机器上的主要内容,如果未能解决你的问题,请参考以下文章

HttpWebRequest.GetRequestStream() 超时

GetRequestStream 随机抛出 Timeout 异常

通过 http 异步发送数据

GetRequestStream 无法连接到服务器 目标计算机积极拒绝 127.0.0.1 1080

HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法

转载HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法