HttpWebRequest和HttpWebResponse问题 [英] HttpWebRequest & HttpWebResponse issues

查看:80
本文介绍了HttpWebRequest和HttpWebResponse问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用HttpWebRequest&连接到服务器HttpWebResponse,它工作正常,但是我遇到另一个问题,我想知道服务器何时超时或断开连接,假设我的连接发生了问题,并且断开了连接,我想知道如何在下面的代码中理解这一点: /p>

I have tried to connect to a server with HttpWebRequest & HttpWebResponse and it works fine, but I got another problem I want to know when the server have been time out or disconnected, suppose something happened to my connection and I got disconnected I want to know how can I understand this in the following code:

string uri = @"myUrl";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Credentials = new NetworkCredential(User, Pass);
        ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
        byte[] buf = new byte[10000];
        int count = -1;
        String read = "";
        HttpWebResponse response;
        //MessageBox.Show("OK");
        //response = (HttpWebResponse)request.GetResponse();
        //count = response.GetResponseStream().Read(buf, 0, buf.Length);
        //read = Encoding.UTF8.GetString(buf, 0, count);
        //MessageBox.Show(read + "SALAM");
        //while (true)
        //{
        response = (HttpWebResponse)request.GetResponse();
        //while (true)
        //{
        do
        {
            count = response.GetResponseStream().Read(buf, 0, buf.Length);
            read += Encoding.UTF8.GetString(buf, 0, count);
        } while (response.GetResponseStream().CanRead && count != 0);

        if (read != "")
        {
            // MessageBox.Show(read);
            XDocument xdoc = XDocument.Parse(read);

            //Filter EventXML
            var lv1s = from lv1 in xdoc.Descendants("event")
                       select new
                       {
                           Event_id = lv1.Attribute("id").Value,
                           Header = lv1.Attribute("name").Value,
                           Children = lv1.Descendants("argument")
                       };
            List<event_details> event_detail = new List<event_details>();


            foreach (var lv1 in lv1s)
            {
                if (lv1.Event_id == event_id)
                    foreach (var lv2 in lv1.Children)
                    {
                        event_details x = new event_details();
                        x.type = lv2.Attribute("type").Value;
                        x.value = lv2.Attribute("value").Value;
                        event_detail.Add(x);
                    }
            }
            //inja chun ke daram rooye MsgDGV ke ye k Datagridview minevisam bayad hatman az Invoke estefade konam
            // ta kharabkari nashe:P:D
            Point detail_point = new Point();
            detail_point.X = MsgDGV.Width / 2 + (this.Width - MsgDGV.Width) / 2;
            detail_point.Y = MsgDGV.Height / 2 + (this.Height - MsgDGV.Height) / 2;
            Details detail = new Details(event_detail, timestamp, EVENT, detail_point);
            detail.ShowDialog();
            event_details.Abort();
        }

推荐答案

实际上我找到了方法!,当您断开与互联网的连接或连接出现问题时,上面的两个答案都可以正常工作抛出一个异常,并且使用上述指定的方式,我们可以解决它,但是当您连接在一起并且在其中途断开连接时,情况发生了变化.由于您已建立联系,因此您可以到达:

Actually I found the way!!, The two answers above are working fine when you are disconnected from the internet or there is some problem with your connection, and it throws an exception and with the ways specified above, we can solve it, but when you are connected and in the middle of that you got disconnected the situation changed. Since you were connected, and you reach the:

response.GetResponseStream().Read(buf, 0, buf.Length);

然后它将停留在此函数中,然后对于读取,您应指定一个超时,以便C#做到这一点:

Then it will stuck in this function then for the read you should specify a timeout so C# got this:

response.GetResponseStream().ReadTimeout = 1000;

因此,在读取之前,您应该指定一个超时时间,然后一切正常;

so before the read you should specify a timeout and then everything works fine;

这篇关于HttpWebRequest和HttpWebResponse问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆