C# 第二次我用;getResponse();在HttpPost中,它只能一步一步地调试

C# 第二次我用;getResponse();在HttpPost中,它只能一步一步地调试,c#,visual-studio-2010,debugging,http-post,C#,Visual Studio 2010,Debugging,Http Post,我在VS2010的框架4.0上使用C#。 我编写了一个控制台应用程序,它按顺序进行两个Http Post调用。 第二个调用在输入中使用第一个调用返回的内容。 好吧,当我在调试模式下运行应用程序时,使用断点一步一步(F10),一切正常。 但是,如果我删除断点并按“F5”,当我的代码在第二个Http Post调用中执行“webRequest.getResponse()”时会出现异常,可能是因为超时,因为出现错误大约需要60秒 例外情况是:错误代码10054-连接被远程主机强制关闭 这是我的应用程序使

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

例外情况是:错误代码10054-连接被远程主机强制关闭

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


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

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


参考资料:

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

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


参考资料:

我使用了跟踪,但没有获得任何额外信息。为了避免WSAECONNRESET,我还尝试了以下方法:设置“HKEY\U LOCAL\U MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort=50000(十进制)”,但没有解决任何问题。还有其他一些想法吗?我尝试使用WireShark嗅探来自PC的HTTP POST调用,发现第二个HTTP调用(检查结果)甚至无法启动。我确信我的WebRequest每次都被正确初始化,但我仍然会出错。你能在第一次和第二次呼叫之间设置一个小的时间延迟吗。例:线程睡眠(3000);延迟3秒。我终于发现了问题所在:Avast antivirus!它阻止了我的第二个http帖子!!我卸载了它,现在一切正常!美好的防病毒有时会导致开发问题。如果你能找出为什么第二个请求被阻止,而第一个请求成功了,那就太好了。我使用了跟踪,但我没有得到任何额外的信息。为了避免WSAECONNRESET,我还尝试了以下方法:设置“HKEY\U LOCAL\U MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort=50000(十进制)”,但没有解决任何问题。还有其他一些想法吗?我尝试使用WireShark嗅探来自PC的HTTP POST调用,发现第二个HTTP调用(检查结果)甚至无法启动。我确信我的WebRequest每次都被正确初始化,但我仍然会出错。你能在第一次和第二次呼叫之间设置一个小的时间延迟吗。例:线程睡眠(3000);延迟3秒。我终于发现了问题所在:Avast antivirus!它阻止了我的第二个http帖子!!我卸载了它,现在一切正常!美好的防病毒有时会导致开发问题。如果你能找出为什么第二个请求被阻止而第一个请求被通过,那就太好了。你真的需要调用
webRequest.Abort()?您真的需要调用
webRequest.Abort()
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 
    }
}