我第二次使用“;getResponse()";在HttpPost中,它只能一步一步地进行调试

本文关键字:一步 调试 HttpPost 第二次 getResponse quot | 更新日期: 2023-09-27 18:19:56

我在VS2010 FrameWork 4.0上使用C#。我编写了一个控制台应用程序,它按顺序进行两次HttpPost调用。第二个调用在输入中使用第一个调用返回的内容。好吧,当我在调试模式下使用断点逐步(F10)运行应用程序时,一切都很好。但是,如果我删除断点并按"F5",当我的代码在SECOND Http Post调用中执行"webRequest.getResponse()"时,我会得到一个异常,可能是因为超时,因为错误大约需要60秒才能出现。

异常为:错误代码10054-远程主机强制关闭了连接。

这是我的应用程序使用的类(控制台应用程序调用方法"Search"):

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

我第二次使用“;getResponse()";在HttpPost中,它只能一步一步地进行调试

您是否尝试查看事件日志中是否存在任何错误?请尝试在您的服务上启用"跟踪"以了解确切原因。发生上述错误有以下描述:

对等方重置连接。远程主机已强制关闭现有连接。如果远程主机上的对等应用程序突然停止、主机重新启动、主机或远程网络接口被禁用或远程主机使用硬关闭,则通常会出现这种情况(有关远程套接字上的SO_LINGER选项的更多信息,请参阅setsockopt)。如果在进行一个或多个操作时,由于保持活动检测到故障而导致连接中断,也可能导致此错误。使用WSAENETRESET进行的操作失败。WSAECONNRESET的后续操作失败。

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