简体   繁体   English

第二次我在HttpPost中使用“ getResponse()”时,它只能逐步调试

[英]The second time I use “getResponse()” in an HttpPost, it works only debugging step by step

I'm using C# on VS2010, FrameWork 4.0. 我在VS2010,FrameWork 4.0上使用C#。 I wrote a Console Application that makes two Http-Post calls in sequence. 我编写了一个控制台应用程序,该应用程序按顺序进行了两个Http-Post调用。 The second call uses in input what is returned by the first. 第二个调用在输入中使用第一个调用返回的内容。 Well, when I run the Application in debug mode, step-by-step (F10) using breakpoints, all works fine. 好吧,当我在调试模式下使用断点分步(F10)运行应用程序时,一切正常。 But if I remove the breakpoints and press "F5", I get an exception when my code executes "webRequest.getResponse()" in the SECOND Http-Post call, probably because of a timeout, because the error takes about 60 seconds to appear. 但是,如果我删除断点并按“ F5”,则我的代码在第二个Http-Post调用中执行“ webRequest.getResponse()”时会出现异常,这可能是因为超时,因为该错误大约需要60秒才会出现。

The exception is: ErrorCode 10054 - Connection was forcibly closed by the remote host. 例外是:错误代码10054-远程主机强行关闭了连接。

This is the class used by my Application (the console application calls the method "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 
    }
}

Have you tried to see if there are any errors in the eventlog? 您是否尝试查看事件日志中是否有任何错误? Try enabling Tracing on your service to know the exact reason. 尝试在您的服务上启用跟踪 ,以了解确切原因。 The above error occurs has the following description : 发生以上错误有以下描述:

Connection reset by peer. 对等方重置连接。 An existing connection was forcibly closed by the remote host. 远程主机强行关闭了现有连接。 This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). 如果远程主机上的对等应用程序突然停止,主机重新启动,主机或远程网络接口被禁用,或者远程主机使用硬关闭(请参阅setsockopt,以获取有关远程SO_LINGER选项的更多信息,通常会导致这种情况)插座)。 This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. 如果在进行一个或多个操作时由于保持活动状态检测到故障而导致连接断开,也可能导致此错误。 Operations that were in progress fail with WSAENETRESET. 正在进行的操作因WSAENETRESET而失败。 Subsequent operations fail with WSAECONNRESET. 后续操作因WSAECONNRESET而失败。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 写入文件仅在“步入”调试模式下工作? - Writing to file works in “step in” debug mode only? 我的代码只能在“进入”中正常运行,而不能正常运行 - My code only works properly in 'step into' and not normally 创建HttpNotificationChannel时获取异常,但在逐步调试时可以正常工作 - Getting exception when creating HttpNotificationChannel, but works correctly when debugging step-by-step 调试 - 下一步? - Debugging - Next step? XmlSerializer仅在“不调试启动”而不是“进入”时崩溃 - XmlSerializer crashing only when “Start without debugging”, but not “Step Into” 如何逐步使用功能 - How to use functions step by step 在非开发机器上使用逐步调试所需的最低要求 - Minimum needed to use Step-by-Step debugging on non-development machine 远程调试Visual Studio 2010可以使用断点,监视并进入吗? - Can remote debugging Visual Studio 2010 use breakpoint, watches and step into? 调试时,我可以进入我的NuGet包 - When debugging I can step into my NuGet package 图片框一步一步工作正常,但执行时,似乎每次执行两次定时器滴答 - pictureBox works fine step by step, but when executed, it seems to execute the timer tick twice each time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM