public static void GetPage(String url)
{
try
{
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// 有些网站会阻止程序访问,需要加入下面这句
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
myHttpWebRequest.Method = "GET";
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}", myHttpWebResponse.StatusDescription);
// Releases the resources of the response.
myHttpWebResponse.Close();
}
catch(WebException e)
{
Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}
}
HttpStatusCode 枚举的值,可以参考:
| 成员名称 | 说明 | |
|---|---|---|
| Accepted | Accepted 指示请求已被接受做进一步处理。 | |
| Ambiguous |
默认操作是将此状态视为重定向,并遵循与此响应关联的 Location 标头的内容。
Ambiguous 将导致引发异常。 MultipleChoices 的同义词。 |
|
| BadGateway | BadGateway 指示中间代理服务器从另一代理或原始服务器接收到错误响应。 | |
| BadRequest | BadRequest。 | |
| Conflict | Conflict 指示由于服务器上的冲突而未能执行请求。 | |
| Continue | Continue 指示客户端可能继续其请求。 | |
| Created | Created 指示请求导致在响应被发送前创建新资源。 | |
| ExpectationFailed | ExpectationFailed 指示服务器未能符合 Expect 头中给定的预期值。 | |
| Forbidden | Forbidden 指示服务器拒绝满足请求。 | |
| Found |
原始请求方法为 POST 时,重定向的请求将使用 GET 方法。
Found 将导致引发异常。 Redirect 的同义词。 |
|
| GatewayTimeout | GatewayTimeout 指示中间代理服务器在等待来自另一个代理或原始服务器的响应时已超时。 | |
| Gone | Gone 指示请求的资源不再可用。 | |
| HttpVersionNotSupported | HttpVersionNotSupported 指示服务器不支持请求的 HTTP 版本。 | |
| InternalServerError | InternalServerError 指示服务器上发生了一般错误。 | |
| LengthRequired | LengthRequired 指示缺少必需的 Content-length 头。 | |
| MethodNotAllowed | MethodNotAllowed 指示请求的资源上不允许请求方法(POST 或 GET)。 | |
| Moved |
原始请求方法为 POST 时,重定向的请求将使用 GET 方法。
MovedPermanently 的同义词。 |
|
| MovedPermanently |
接收到此状态时的默认操作为遵循与响应关联的 Location 头。
Moved 的同义词。 |
|
| MultipleChoices |
默认操作是将此状态视为重定向,并遵循与此响应关联的 Location 标头的内容。
MultipleChoices 将导致引发异常。 Ambiguous 的同义词。 |
|
| NoContent | NoContent 指示已成功处理请求并且响应已被设定为无内容。 | |
| NonAuthoritativeInformation | NonAuthoritativeInformation 指示返回的元信息来自缓存副本而不是原始服务器,因此可能不正确。 | |
| NotAcceptable | NotAcceptable 指示客户端已用 Accept 头指示将不接受资源的任何可用表示形式。 | |
| NotFound | NotFound 指示请求的资源不在服务器上。 | |
| NotImplemented | NotImplemented 指示服务器不支持请求的函数。 | |
| NotModified | 未传输此资源的内容。 | |
| OK | 这是最常接收的状态代码。 | |
| PartialContent | PartialContent 指示响应是包括字节范围的 GET 请求所请求的部分响应。 | |
| PaymentRequired | PaymentRequired 以供将来使用。 | |
| PreconditionFailed | 条件是用条件请求标头(如 If-Match、If-None-Match 或 If-Unmodified-Since)设置的。 | |
| ProxyAuthenticationRequired | Proxy-authenticate 头包含如何执行身份验证的详细信息。 | |
| Redirect |
原始请求方法为 POST 时,重定向的请求将使用 GET 方法。
Redirect 将导致引发异常。 Found 的同义词。 |
|
| RedirectKeepVerb |
原始请求方法为 POST 时,重定向的请求还将使用 POST 方法。
RedirectKeepVerb 将导致引发异常。 TemporaryRedirect 的同义词。 |
|
| RedirectMethod |
用 GET 生成对 Location 标头所指定的资源的请求。
RedirectMethod 将导致引发异常。 SeeOther 的同义词。 |
|
| RequestedRangeNotSatisfiable | RequestedRangeNotSatisfiable 指示无法返回从资源请求的数据范围,因为范围的开头在资源的开头之前,或因为范围的结尾在资源的结尾之后。 | |
| RequestEntityTooLarge | RequestEntityTooLarge 指示请求太大,服务器无法处理。 | |
| RequestTimeout | RequestTimeout 指示客户端没有在服务器期望请求的时间内发送请求。 | |
| RequestUriTooLong | RequestUriTooLong 指示 URI 太长。 | |
| ResetContent | ResetContent 指示客户端应重置(或重新加载)当前资源。 | |
| SeeOther |
用 GET 生成对 Location 标头所指定的资源的请求。
SeeOther 将导致引发异常。 RedirectMethod 的同义词。 |
|
| ServiceUnavailable | ServiceUnavailable 指示服务器暂时不可用,通常是由于过多加载或维护。 | |
| SwitchingProtocols | SwitchingProtocols 指示正在更改协议版本或协议。 | |
| TemporaryRedirect |
原始请求方法为 POST 时,重定向的请求还将使用 POST 方法。
TemporaryRedirect 将导致引发异常。 RedirectKeepVerb 的同义词。 |
|
| Unauthorized | WWW-Authenticate 头包含如何执行身份验证的详细信息。 | |
| UnsupportedMediaType | UnsupportedMediaType 指示请求是不支持的类型。 | |
| Unused | Unused 是未完全指定的 HTTP/1.1 规范的建议扩展。 | |
| UpgradeRequired | UpgradeRequired 指示客户端应切换为诸如 TLS/1.0 之类的其他协议。 | |
| UseProxy | UseProxy 指示请求应使用位于 Location 头中指定的 URI 的代理服务器。 |