2012-01-10 92 views
2

我在VS2010,FrameWork 4.0上使用C#。 我写了一个控制台应用程序,它依次发出两个Http-Post调用。 第二个调用在输入中使用第一个返回的内容。 那么,当我在调试模式下运行应用程序,使用断点一步一步(F10),一切正常。 但是,如果我删除了断点并按下“F5”,当我的代码在SECOND Http-Post调用中执行“webRequest.getResponse()”时可能会因为超时而收到异常,因为该错误大约需要60秒出现。我第二次在HttpPost中使用“getResponse()”,它只能一步一步地调试

例外情况是:ErrorCode 10054 - 连接被远程主机强制关闭。

这是使用我的应用程序的类(控制台应用程序调用方法“搜索”):

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Xml; 
using System.Net; 
using System.IO; 

namespace MyNamespace 
{ 
    public class MyClass 
    { 
     private string SearchId { get; set; } 
     private string XmlOutputSearch { get; set; } 

     public MyClass() 
     { 
      SearchId = ""; 
      XmlOutputSearch = ""; 
     } 

     public string Search() 
     { 
      StartSearch(); 
      CheckResults(); 
      return XmlOutputSearch; 
     } 

     public void StartSearch() 
     { 
      string sInput = "[myStartSearchXmlRequest]"; 
      string sOutput = HttpPost(sInput); 

      XmlDocument myXmlDoc = new XmlDocument(); 
      myXmlDoc.LoadXml(sOutput); 
      SearchId = myXmlDoc.SelectSingleNode("//SearchId").InnerXml; 
     } 

     public void CheckResults() 
     { 
      string sInput = "[myCheckResultsXmlRequest using SearchId]"; 
      XmlOutputSearch = HttpPost(sInput); 
     } 

     private string HttpPost(string parameters) 
     { 
      try 
      { 
       HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("[myURI]"); 

       webRequest.ContentType = "text/xml"; 
       webRequest.Method = "POST"; 

       byte[] bytes = Encoding.ASCII.GetBytes(parameters); 
       Stream os = null; 
       try 
       { // send the Post 
        webRequest.ContentLength = bytes.Length; //Count bytes to send 
        os = webRequest.GetRequestStream(); 
        os.Write(bytes, 0, bytes.Length);   //Send it 
       } 

       catch (WebException ex) 
       { 
        throw ex; 
       } 
       finally 
       { 
        if (os != null) 
        { 
         os.Close(); 
        } 
       } 

       try 
       { // get the response 

        // Here I get the exception, on webRequest.GetResponse(), when HttpPost is called 
        // by CheckResults, if not in Step-By-Step mode 
        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
        { 
         if (webResponse == null) 
         { return null; } 
         StreamReader sr = new StreamReader(webResponse.GetResponseStream()); 
         string sReturn = sr.ReadToEnd().Trim(); 
         sr.Close(); 
         webResponse.Close(); 
         webRequest.Abort(); 

         return sReturn; 
        } 
       } 
       catch (WebException wex) 
       { 
        // This exception will be raised if the server didn't return 200 - OK 
        // Try to retrieve more information about the network error 
        if (wex.Response != null) 
        { 
         using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response) 
         { 
          Console.WriteLine(
           "The server returned '{0}' with the status code {1} ({2:d}).", 
           errorResponse.StatusDescription, errorResponse.StatusCode, 
           errorResponse.StatusCode); 
         } 
        } 
       } 
      } 
      catch (Exception excep) 
      { 
       Console.WriteLine("Exception in WebResponse. " + excep.Message + " - " + excep.StackTrace); 
      } 
      return null; 
     } // end HttpPost 
    } 
} 
+1

你真的需要被调用'webRequest.Abort();'? – 2012-01-11 20:59:04

回答

0

你想看看是否有在事件日志的任何错误?尝试在您的服务上启用Tracing以了解确切原因。发生上述错误的描述如下:

由对等体重置连接。 现有连接被远程主机强制关闭。如果远程主机上的对等应用程序突然停止,主机重新启动,主机或远程网络接口被禁用,或者远程主机使用硬关闭,则通常会导致此结果(有关远程设备上的SO_LINGER选项的更多信息,请参阅setsockopt插座)。如果由于保活活动在一个或多个操作正在进行时检测到故障而导致连接中断,则也可能导致此错误。正在进行的操作由于WSAENETRESET而失败。随后的操作会失败,并显示WSAECONNRESET。

参考:http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

+0

感谢您的回答。 – 2012-01-10 11:34:39

+0

我使用了追踪,但我没有得到任何额外的信息。为了避免WSAECONNRESET,我也试过这个:我设置“HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ Tcpip \ Parameters \ MaxUserPort = 50000(十进制)”,但没有解决任何问题。 其他一些想法? – 2012-01-10 11:42:11

+0

有没有人有任何想法来解决我的问题? – 2012-01-11 08:28:09