【问题标题】:httpwebrequest gives timeout until restartedhttpwebrequest 给出超时直到重新启动
【发布时间】:2011-04-30 06:49:25
【问题描述】:

我正在开发一个用 C#(.NET 环境)开发的桌面应用程序。

此应用程序使用 HttpWebRequest 连接到远程服务器。如果由于任何原因我的 PC 与 Internet 断开连接并且我重新连接它,我的应用程序总是为 HttpWebRequest 提供请求超时,直到我重新启动整个应用程序并且如果我在之后再次向我的应用程序添加新线程网络 d/c 工作正常。

有什么方法可以重置我的网络,或者任何人都可以告诉我它是如何工作的?

//我的代码是..

public String request(String add, String post, int time, String reff, int id, int rwtime)
    {
        try
        {
            if (rwtime == 0)
            {
                rwtime = 100000;
            }
            string result = "";
            string location = "";
            // Create the web request
            HttpWebRequest req = WebRequest.Create(add) as HttpWebRequest;
            req.ReadWriteTimeout = rwtime;
            req.KeepAlive = true;
            req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Timeout = time;
            req.Referer = reff;
            req.AllowAutoRedirect = false;
            req.CookieContainer = statictk.cc[id];
            req.PreAuthenticate = true;
            if (post != "")
            {
                req.Method = "POST";
                string postData = post;
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] byte1 = encoding.GetBytes(postData);
                // Set the content type of the data being posted.
                req.ContentType = "application/x-www-form-urlencoded";
                // Set the content length of the string being posted.
                req.ContentLength = byte1.Length;
                Stream newStream = req.GetRequestStream();
                newStream.Write(byte1, 0, byte1.Length);
                newStream.Close();
            }
            else
            {
                req.Method = "GET";
            }
            // Get response
            try
            {
                HttpWebResponse response = req.GetResponse() as HttpWebResponse;

                // Get the response stream
                location = response.GetResponseHeader("Location");
                if (location == "")
                {
                    Stream responseStream = response.GetResponseStream();
                    if (response.ContentEncoding.ToLower().Contains("gzip"))
                        responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                    else if (response.ContentEncoding.ToLower().Contains("deflate"))
                        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);

                    StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                    // Read the whole contents and return as a string
                    result = reader.ReadToEnd();
                }
                else
                {
                    result = location;
                }
                response.Close();
                if (result == "") result = "retry";
                return result;
            }

            catch (Exception e)
            {
                log.store("errorinresponce", e.Message);
                if (statictd.status[id] != "removed")
                {
                    return "retry";
                }
                else
                {
                    return "error";
                }
            }
        }
        catch(Exception f)
        {
            log.store("Networkerrorretry", f.Message);
            if (f.Message == "The operation has timed out")
            {
                return "retry";
            }
            string ans = MessageBox.Show("There was a Network Error..Wish to Retry ?\nError msg : "+ f.Message, "Title", MessageBoxButtons.YesNo).ToString();
            if (ans == "Yes")
                return "retry";
            else
            {
                Invoketk.settxt(id, "Not Ready");
                return "error";
            }
        }
    }

【问题讨论】:

    标签: c# network-programming desktop-application


    【解决方案1】:

    听起来您的应用程序缺少一些错误处理。任何时候都可能发生断开连接,您的应用程序应该能够处理它。尝试用 try-catch 语句包围网络循环,然后捕获不同类型的异常。根据引发的异常,您可以决定是静默重新连接到服务器还是要生成错误消息。

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 2017-05-31
      • 1970-01-01
      • 2015-01-09
      • 2019-04-17
      • 2011-03-21
      • 2015-09-30
      • 2019-02-17
      相关资源
      最近更新 更多