如何在c#中等待并重复Web响应以获取错误Web响应? 什么时候做Web响应,错误得到响应。我想尝试网络响应没有错误并继续程序与成功获取响应网址。 我的代码:
try
{
HttpWebRequest URLp =(HttpWebRequest)WebRequest.Create(url);
URLp.Timeout = 190000;
URLp.Timeout = 260000;
URLp.ReadWriteTimeout = 260000;
using (WebResponse MyResponse1 = URLp.GetResponse())
{
str1 = new StreamReader(MyResponse1.GetResponseStream(), System.Text.Encoding.UTF8);
page1 = str1.ReadToEnd();
}
}
catch
{
Thread.Sleep(70000);
}
StreamWriter stw1 = new StreamWriter(Address);
stw1.Write(page1);
stw1.Close();
答案 0 :(得分:1)
你的意思是这样的吗?
var success = false;
while (!success)
{
try
{
HttpWebRequest URLp =(HttpWebRequest)WebRequest.Create(url);
URLp.Timeout = 190000;
URLp.Timeout = 260000;
URLp.ReadWriteTimeout = 260000;
using (WebResponse MyResponse1 = URLp.GetResponse())
{
str1 = new StreamReader(MyResponse1.GetResponseStream(), System.Text.Encoding.UTF8);
page1 = str1.ReadToEnd();
success = true;
}
}
catch
{
Thread.Sleep(70000);
}
}
StreamWriter stw1 = new StreamWriter(Address);
stw1.Write(page1);
stw1.Close();