簡體   English   中英

第二次我在HttpPost中使用“ getResponse()”時,它只能逐步調試

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

我在VS2010,FrameWork 4.0上使用C#。 我編寫了一個控制台應用程序,該應用程序按順序進行了兩個Http-Post調用。 第二個調用在輸入中使用第一個調用返回的內容。 好吧,當我在調試模式下使用斷點分步(F10)運行應用程序時,一切正常。 但是,如果我刪除斷點並按“ F5”,則我的代碼在第二個Http-Post調用中執行“ webRequest.getResponse()”時會出現異常,這可能是因為超時,因為該錯誤大約需要60秒才會出現。

例外是:錯誤代碼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 
    }
}

您是否嘗試查看事件日志中是否有任何錯誤? 嘗試在您的服務上啟用跟蹤 ,以了解確切原因。 發生以上錯誤有以下描述:

對等方重置連接。 遠程主機強行關閉了現有連接。 如果遠程主機上的對等應用程序突然停止,主機重新啟動,主機或遠程網絡接口被禁用,或者遠程主機使用硬關閉(請參閱setsockopt,以獲取有關遠程SO_LINGER選項的更多信息,通常會導致這種情況)插座)。 如果在進行一個或多個操作時由於保持活動狀態檢測到故障而導致連接斷開,也可能導致此錯誤。 正在進行的操作因WSAENETRESET而失敗。 后續操作因WSAECONNRESET而失敗。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM