HttpWebRequest & HttpWebResponse issues
本文关键字:HttpWebResponse issues amp HttpWebRequest | 更新日期: 2023-09-27 18:22:23
我尝试使用HttpWebRequest连接到服务器;HttpWebResponse,它工作得很好,但我遇到了另一个问题,我想知道服务器何时超时或断开连接,假设我的连接发生了问题,我断开了连接。我想知道如何在以下代码中理解这一点:
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();
}
当Request.GetResponse()方法调用TimesOut时,您需要捕获在调用HttpWebRequest.GetResponse方法时引发的WebException。GetResponse()方法可以抛出四个异常,因此您需要检查抛出的异常的类型或捕获所需的特定异常类型,即:catch(WebException ex){}。
请注意,您可以根据需要获取和设置WebRequest.Timeout属性。
// Set the 'Timeout' property in Milliseconds.
request.Timeout = 10000;
在您的代码中,您将把HttpWebRequest.GetResponse()方法调用和所有与GetResponce()调用暴露的数据相关的代码包装在Try-Catch块中。您还应该利用WebResponse实现IDisposable的事实,并使用using语法来管理对象范围和生存期,这样就不会留下对不再需要或不在范围内的对象的引用。
try
{
using (WebResponse response = request.GetResponse())
{
// ALL OTHER CODE
}
}
catch (Exception ex)
{
// Handle Exception
}
实际上我找到了路!!,当你与互联网断开连接或连接出现问题时,上面两个答案都很好,这会引发异常,通过上面指定的方法,我们可以解决它,但当你与网络断开连接时,情况发生了变化。由于您已连接,并且您已到达:
response.GetResponseStream().Read(buf, 0, buf.Length);
然后它会卡在这个函数中,然后为了读取,你应该指定一个超时,所以C#得到了这个:
response.GetResponseStream().ReadTimeout = 1000;
因此,在读取之前,您应该指定一个超时,然后一切正常;
您可以捕获WebException
,以查看请求执行期间是否发生错误或请求的超时期是否已过期:
try
{
using(var response = (HttpWebResponse)request.GetResponse())
{
}
}
catch(WebException e)
{
//timeout or error during execution
}
此外,您可能需要检查响应状态是否等于200:
if(resp.StatusCode == 200)
{
//code
}
有关HttpRequest的更多详细信息,您可以在此处找到