提问人:Digital3D 提问时间:8/21/2023 更新时间:8/21/2023 访问量:21
HttpWebRequest:自动重定向过多(临时移动了 302)
HttpWebRequest: Too many automatic redirections (302 Moved Temporarily)
问:
我尝试从“节目电视”网站下载一些网页,它以前有效,但现在有些事情发生了变化,无法从该站点下载页面,网址为:https://www.programme-tv.net/programme/chaines-tv.html
这是我的代码:
//url = https://www.programme-tv.net/programme/chaines-tv.html
public async Task<PageResponse> GetPage(string url, int repeat = 0, string moreInfoForLog = null)
{
PageResponse pageResponse = new PageResponse();
Thread.Sleep(100);
ServicePointManager.Expect100Continue = false;
ServicePointManager.MaxServicePointIdleTime = 30000;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += TlsValidationCallback;
string result = null;
HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
try
{
webReq.CookieContainer = new CookieContainer();
webReq.AllowAutoRedirect = true;
webReq.KeepAlive = true;
webReq.MaximumAutomaticRedirections = 50;
webReq.ConnectionGroupName = Guid.NewGuid().ToString();
webReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
webReq.Method = "GET";
webReq.UserAgent = GetUserAgent(); //get random user Agent from another method
webReq.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webReq.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8,es-ES;q=0.5,en;q=0.3");
webReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8");
webReq.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
using (WebResponse response = await webReq.GetResponseAsync())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
result = await reader.ReadToEndAsync();
pageResponse.Html = result;
}
}
}
catch (WebException ex)
{
HttpWebResponse errorResponse = ex.Response as HttpWebResponse;
if (errorResponse != null && errorResponse.StatusCode == HttpStatusCode.NotFound || ex.Status == WebExceptionStatus.ConnectionClosed)
{
pageResponse.isError = true;
pageResponse.Message = $"NOT FOUND {url}: {ex.Message}";
_log.Debug(pageResponse.Message);
}
else if (repeat < 5 && errorResponse != null && (errorResponse.StatusCode == HttpStatusCode.Redirect || errorResponse.StatusCode == HttpStatusCode.MovedPermanently))
{
url = errorResponse.Headers["Location"];
return await GetPage(url, repeat, moreInfoForLog);
}
else if (repeat < 5)
{
Thread.Sleep(5000);
repeat++;
return await GetPage(url, repeat, moreInfoForLog);
}
else
{
pageResponse.isError = true;
pageResponse.Message = $"WEB EXCEPTION ERROR {url} tried 5 times: {ex.Message}";
}
}
catch (Exception ex)
{
if (repeat < 5)
{
Thread.Sleep(5000);
repeat++;
return await GetPage(url, repeat, moreInfoForLog);
}
else
{
pageResponse.isError = true;
pageResponse.Message = $"GENERAL ERROR {url} tried 5 times: {ex.Message}";
}
}
return pageResponse;
}
private bool TlsValidationCallback(object sender, X509Certificate CACert, X509Chain CAChain, SslPolicyErrors sslPolicyErrors)
{
if ((sslPolicyErrors == SslPolicyErrors.None))
return true;
X509Certificate2 _Certificate = new X509Certificate2(CACert);
CAChain.Build(_Certificate);
foreach (X509ChainStatus CACStatus in CAChain.ChainStatus)
{
if ((CACStatus.Status != X509ChainStatusFlags.NoError) & (CACStatus.Status != X509ChainStatusFlags.UntrustedRoot))
return false;
}
return true;
}
GetPage 方法返回一个简单的对象:
public class PageResponse
{
public string Html { get; set; }
public bool isError { get; set; }
public string Message { get; set; }
}
现在返回的错误始终是:
尝试了太多的自动重定向。
我在stackoverflow上阅读了有关此错误的所有票证,但没有人帮助我,我尝试了一切!
感谢您的帮助,我很好奇您是否能找到解决方案!:)
答: 暂无答案
评论