【发布时间】:2018-08-28 15:30:47
【问题描述】:
所以我刚刚开始学习 HttpWebRequests 及其功能。 我已经到了想学习如何在 CookieContainer 中捕获 cookie 并解析它们的地步。
问题是某些网站返回 503 错误,我不确定。 本示例中将使用其中一个网站。 从我在线阅读的内容来看,这是一个 503 错误。
超文本传输协议 (HTTP) 503 服务不可用服务器 错误响应代码表示服务器尚未准备好处理 请求。
常见原因是服务器因维护而停机或 超载。此响应应用于临时条件和 如果可能,Retry-After HTTP 标头应包含估计的 服务恢复时间。
自从网站启动并运行以来,这似乎根本不适合。 为什么我的请求会返回 503 状态码,我应该如何以适当的方式解决此问题?
static void Main(string[] args)
{
//1. Create a HTTP REQUEST
//Build the request
Uri site = new Uri("https://ucp.nordvpn.com/login/");
//Inizializing a new instance of the HttpWebRequest and casting it as a WebRequest
//And calling the Create function and using our site as a paramter which the Create function takes.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(site);
//Inizialize a new instance of the CookieContainer
CookieContainer cookies = new CookieContainer();
//The request has a CookieContainer, which is null by default, so we are just assinging the newly inizialized instance
//of our CookieContainer to our requests CookieContainer
request.CookieContainer = cookies;
//Print out the number of cookies before the response (of course it will be blank)
Console.WriteLine(cookies.GetCookieHeader(site));
//Get the response and print out the cookies again
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(cookies.GetCookieHeader(site));
}
Console.ReadKey();
}
【问题讨论】:
-
HTTPS 正在使用中。搜索
ServicePointManager.SecurityProtocol和ServicePointManager.ServerCertificateValidationCallback。当您收到503时,您应该检查接收流的响应主体(如果不为空)。应包含一条消息 (Html),以便您更好地了解问题所在。
标签: c# .net httpwebrequest httpwebresponse