为什么我得到异常:有太多的自动重定向已尝试在Web客户端? [英] Why i'm getting exception: Too many automatic redirections were attempted on webclient?
问题描述
在Form1上我做的顶部:
WebClient的客户端;
然后在构造函数中:
客户端=新的WebClient();
Client.DownloadFileCompleted + = Client_DownloadFileCompleted;
Client.DownloadProgressChanged + = Client_DownloadProgressChanged;
然后,我有这个方法我打电话每分钟:
私人无效fileDownloadRadar()
{
如果(Client.IsBusy ==真)
{
Client.CancelAsync ();
}
,否则
{
Client.DownloadProgressChanged + = Client_DownloadProgressChanged;
Client.DownloadFileAsync(myUri,combinedTemp);
}
}
每分钟它是从一个网站相同的图像下载图像每一次。
这是所有工作更多然后24小时没有问题,现在直到抛出该异常在下载完成的事件:
私人无效Client_DownloadFileCompleted(对象发件人,AsyncCompletedEventArgs E)
{
如果(e.Error!= NULL)
{
timer1.Stop();
跨度=新时间跨度(0,(INT)numericUpDown1.Value,0);
label21.Text = span.ToString(@mm\:SS);
timer3.Start();
}
,否则如果
{
label19.ForeColor = Color.Green(e.Cancelled!);
label19.Text =חיבורהאינטרנטוהאתרתקינים
label19.Visible = TRUE;
timer3.Stop();
如果(timer1.Enabled =真!)
{
如果(BeginDownload ==真)
{
timer1.Start();
}
}
布尔fileok = Bad_File_Testing(combinedTemp);
如果(fileok ==真)
{
=文件1新位图(combinedTemp);
布尔比= ComparingImages(文件1);
如果(==相比假)
{
DirectoryInfo的DIR1 =新DirectoryInfo的(SF);
的FileInfo [] F1 = dir1.GetFiles(* GIF。);
last_file =网络连接[fi.Length - 1] .FullName;
串lastFileNumber = last_file.Substring(82,6);
INT lastNumber = int.Parse(lastFileNumber);
lastNumber ++;
串newFileName =的String.Format(雷达{0:D6} .gif注意,lastNumber);
identicalFilesComparison = File_Utility.File_Comparison(combinedTemp,last_file);
如果(identicalFilesComparison ==假)
{
串的newfile = Path.Combine(SF,newFileName);
File.Copy(combinedTemp,newfile中);
LastFileIsEmpty();
}
}
如果(checkBox2.Checked)
{
simdownloads.SimulateDownloadRadar();
}
}
,否则
{
File.Delete(combinedTemp);
}
File1.Dispose();
}
}
现在就停止了,如果(e.Error里面! = NULL)
就行了:timer1.Stop();
然后我看到在错误的错误:
这是堆栈跟踪
在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult的asyncResult)
在System.Net.WebClient.GetWebResponse( WebRequest的要求,IAsyncResult的结果)
在System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult的结果)
我怎样才能解决这个问题,所以它不会再次发生?而为什么会发生
编辑:
我试图fileDownloadRadar方法改变这种释放客户端每次:
使用私人无效fileDownloadRadar()
{
(Web客户端的客户端=新的Web客户端() )
{
如果(client.IsBusy ==真)
{
client.CancelAsync();
}
,否则
{
client.DownloadFileAsync(myUri,combinedTemp);
}
}
}
问题是在构造函数中我使用的客户端和这里的客户两种不同的Web客户端的变量。
我怎样才能解决这个问题和异常?
这是对于我要自行下载每分钟图像的部位websitel墨水。
仍然还不能确定为什么我得到这个例外,这是工作没有问题,更多的则24小时后。
现在我再次运行程序一遍,它的工作,但我不知道我会在接下来的时间再次的明天,有时得到这个例外。
<一个HREF =http://www.ims.gov.il/IMS/tazpiot/RainRadar.htm相对=nofollow>与图像即时下载
网站< DIV CLASS =h2_lin>解决方案
我曾与Web客户端同样的问题,并找到解决方案在这里:
的 http://blog.developers.ba/fixing-issue-httpclient-many-automatic-redirections-attempted/
使用HttpWebRequest和设置的CookieContainer解决了这个问题,例如:
HttpWebRequest的webReq =(HttpWebRequest的) HttpWebRequest.Create(linkUrl);
试
{
webReq.CookieContainer =新的CookieContainer();
webReq.Method =GET;
使用(WebResponse的响应= webReq.GetResponse())
{
使用(流流= response.GetResponseStream())
{
StreamReader的读者=新的StreamReader(流);
解析度= reader.ReadToEnd();
:
}
}
}
赶上(异常前)
{
:
}
In the top of form1 i did:
WebClient Client;
Then in the constructor:
Client = new WebClient();
Client.DownloadFileCompleted += Client_DownloadFileCompleted;
Client.DownloadProgressChanged += Client_DownloadProgressChanged;
Then i have this method i'm calling every minute:
private void fileDownloadRadar()
{
if (Client.IsBusy == true)
{
Client.CancelAsync();
}
else
{
Client.DownloadProgressChanged += Client_DownloadProgressChanged;
Client.DownloadFileAsync(myUri, combinedTemp);
}
}
Every minutes it's downloading an image from a website same image each time. It was all working for more then 24 hours no problems untill now throwing this exception in the download completed event:
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
timer1.Stop();
span = new TimeSpan(0, (int)numericUpDown1.Value, 0);
label21.Text = span.ToString(@"mm\:ss");
timer3.Start();
}
else if (!e.Cancelled)
{
label19.ForeColor = Color.Green;
label19.Text = "חיבור האינטרנט והאתר תקינים";
label19.Visible = true;
timer3.Stop();
if (timer1.Enabled != true)
{
if (BeginDownload == true)
{
timer1.Start();
}
}
bool fileok = Bad_File_Testing(combinedTemp);
if (fileok == true)
{
File1 = new Bitmap(combinedTemp);
bool compared = ComparingImages(File1);
if (compared == false)
{
DirectoryInfo dir1 = new DirectoryInfo(sf);
FileInfo[] fi = dir1.GetFiles("*.gif");
last_file = fi[fi.Length - 1].FullName;
string lastFileNumber = last_file.Substring(82, 6);
int lastNumber = int.Parse(lastFileNumber);
lastNumber++;
string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
identicalFilesComparison = File_Utility.File_Comparison(combinedTemp, last_file);
if (identicalFilesComparison == false)
{
string newfile = Path.Combine(sf, newFileName);
File.Copy(combinedTemp, newfile);
LastFileIsEmpty();
}
}
if (checkBox2.Checked)
{
simdownloads.SimulateDownloadRadar();
}
}
else
{
File.Delete(combinedTemp);
}
File1.Dispose();
}
}
Now it stopped inside the if(e.Error != null) On the line: timer1.Stop();
Then i see on the Error the error: This is the stack trace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
How can i solve this problem so it won't happen again ? And why it happened ?
EDIT:
I tried to change the fileDownloadRadar method to this to release the client every time:
private void fileDownloadRadar()
{
using (WebClient client = new WebClient())
{
if (client.IsBusy == true)
{
client.CancelAsync();
}
else
{
client.DownloadFileAsync(myUri, combinedTemp);
}
}
}
The problem is that in the constructor i'm using Client and here it's client two different Webclient variables.
How can i solve this and the exception ?
This is the websitel ink for the site with the image i'm downloading every minute. Still not sure yet why i got this exception after it was working no problems for more then 24 hours. Now i ran the program again over again and it's working but i wonder if i will get this exception again tommorow or sometimes in the next hours.
The site with image i'm downloading
I had the same problem with WebClient and found the solution here: http://blog.developers.ba/fixing-issue-httpclient-many-automatic-redirections-attempted/
Using HttpWebRequest and setting a CookieContainer solved the problem, for example:
HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(linkUrl);
try
{
webReq.CookieContainer = new CookieContainer();
webReq.Method = "GET";
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
...
}
}
}
catch (Exception ex)
{
...
}
这篇关于为什么我得到异常:有太多的自动重定向已尝试在Web客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!