Phy*_*dha 3 c# http-status-code-404 web
我想在我的程序中获取有关Microsoft Update的信息.但是,服务器在大约80%的时间返回404错误.我将有问题的代码归结为此控制台应用程序:
using System;
using System.Net;
namespace WebBug
{
class Program
{
static void Main(string[] args)
{
while (true)
{
try
{
WebClient client = new WebClient();
Console.WriteLine(client.DownloadString("https://support.microsoft.com/api/content/kb/3068708"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我必须经历几次循环,直到得到实际响应:
远程服务器返回错误:(404)未找到.
远程服务器返回错误:(404)未找到.
远程服务器返回错误:(404)未找到.
<div kb-title title ="客户体验和诊断遥测更新[...]
我可以随时打开并强制刷新(Ctrl + F5)我浏览器中的链接,但它会显示正常.
问题发生在具有两个不同互联网连接的两台不同机器上.
我也使用Html Agility Pack测试了这个案例,但结果相同.其他网站不会出现
此问题.(根部工作正常100%)https://support.microsoft.com
为什么我会得到这个奇怪的结果?
饼干.这是因为饼干.
当我开始深入研究这个问题时,我注意到我第一次在新浏览器中打开网站时得到了404,但在刷新后(有时一次,有时几次)网站继续工作.
那是我淘汰Chrome的Incognito模式和开发者工具的时候.
网络上没有任何可疑的东西:如果你加载了http,就有一个简单的重定向到https版本.
但我注意到的是饼干改变了.这是我第一次加载页面时看到的内容:
这是(或几个)刷新后的页面:
请注意如何添加更多cookie条目?该网站必须试图阅读那些,而不是找到它们,并"阻止"你.这可能是一个防止僵尸的设备或糟糕的编程,我不确定.
无论如何,这里是如何使您的代码工作.此示例使用HttpWebRequest/Response,而不是WebClient.
string url = "https://support.microsoft.com/api/content/kb/3068708";
//this holds all the cookies we need to add
//notice the values match the ones in the screenshot above
CookieContainer cookieJar = new CookieContainer();
cookieJar.Add(new Cookie("SMCsiteDir", "ltr", "/", ".support.microsoft.com"));
cookieJar.Add(new Cookie("SMCsiteLang", "en-US", "/", ".support.microsoft.com"));
cookieJar.Add(new Cookie("smc_f", "upr", "/", ".support.microsoft.com"));
cookieJar.Add(new Cookie("smcexpsessionticket", "100", "/", ".microsoft.com"));
cookieJar.Add(new Cookie("smcexpticket", "100", "/", ".microsoft.com"));
cookieJar.Add(new Cookie("smcflighting", "wwp", "/", ".microsoft.com"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//attach the cookie container
request.CookieContainer = cookieJar;
//and now go to the internet, fetching back the contents
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using(StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string site = sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
如果您删除request.CookieContainer = cookieJar;它,它将失败,404会再现您的问题.