WebRequest 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对统一资源标识符 (URI) 发出请求。 这是一个 abstract
类。
public ref class WebRequest abstract
public ref class WebRequest abstract : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public abstract class WebRequest
public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable
[System.Serializable]
public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable
type WebRequest = class
type WebRequest = class
inherit MarshalByRefObject
interface ISerializable
[<System.Serializable>]
type WebRequest = class
inherit MarshalByRefObject
interface ISerializable
Public MustInherit Class WebRequest
Public MustInherit Class WebRequest
Inherits MarshalByRefObject
Implements ISerializable
- 继承
-
WebRequest
- 继承
- 派生
- 属性
- 实现
示例
以下示例演示如何创建 WebRequest 实例并返回响应。
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Text;
int main()
{
// Create a request for the URL.
WebRequest^ request = WebRequest::Create( "http://www.contoso.com/default.html" );
// If required by the server, set the credentials.
request->Credentials = CredentialCache::DefaultCredentials;
// Get the response.
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
// Display the status.
Console::WriteLine( response->StatusDescription );
// Get the stream containing content returned by the server.
Stream^ dataStream = response->GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader^ reader = gcnew StreamReader( dataStream );
// Read the content.
String^ responseFromServer = reader->ReadToEnd();
// Display the content.
Console::WriteLine( responseFromServer );
// Cleanup the streams and the response.
reader->Close();
dataStream->Close();
response->Close();
}
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Create a request for the URL.
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.
Console.WriteLine (response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();
}
}
}
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
Public Class WebRequestGetExample
Public Shared Sub Main()
' Create a request for the URL.
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
' Display the status.
Console.WriteLine(response.StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Cleanup the streams and the response.
reader.Close()
dataStream.Close()
response.Close()
End Sub
End Class
End Namespace
注解
重要
不要使用 WebRequest
或其派生类进行新开发。 请改用 System.Net.Http.HttpClient 类。
WebRequestabstract
是 的基类。NET 的请求/响应模型,用于从 Internet 访问数据。 使用请求/响应模型的应用程序可以以与协议无关的方式从 Internet 请求数据,即应用程序使用类的 WebRequest 实例,而特定于协议的后代类执行请求的详细信息。
请求从应用程序发送到特定的 URI,例如服务器上的网页。 URI 确定要从为应用程序注册的 WebRequest 后代列表创建的适当后代类。 WebRequest 后代通常注册以处理特定协议(如 HTTP 或 FTP),但可以注册以处理对服务器上特定服务器或路径的请求。
当 WebRequest 访问 Internet 资源时出错时,类会 WebException 引发 。 属性 Status 是指示错误来源的值之一 WebExceptionStatus 。 当 为 WebExceptionStatus.ProtocolError时Status,Response属性包含WebResponse从 Internet 资源接收的 。
WebRequest由于 类是类abstract
,因此实例在运行时的实际行为WebRequest由方法返回Create的后代类确定。 有关默认值和异常的详细信息,请参阅后代类(如 HttpWebRequest 和 FileWebRequest)的文档。
注意
Create使用 方法初始化新WebRequest实例。 请勿使用 WebRequest 构造函数。
注意
如果创建 WebRequest 对象的应用程序使用 Normal 用户的凭据运行,则应用程序将无法访问本地计算机存储中安装的证书,除非已显式授予用户执行此操作的权限。
实施者说明
从 WebRequest继承时,必须重写以下成员:Method、、RequestUri、Headers、ContentTypeContentLength、GetRequestStream()PreAuthenticateCredentialsBeginGetRequestStream(AsyncCallback, Object)、EndGetRequestStream(IAsyncResult)、、GetResponse()、 BeginGetResponse(AsyncCallback, Object)和 。EndGetResponse(IAsyncResult) 此外,必须提供 接口的 IWebRequestCreate 实现,该接口定义 Create(Uri) 调用 Create(Uri)时使用的方法。 必须使用 方法或配置文件注册实现 IWebRequestCreate 接口 RegisterPrefix(String, IWebRequestCreate) 的类。
构造函数
WebRequest() |
已过时。
初始化 WebRequest 类的新实例。 |
WebRequest(SerializationInfo, StreamingContext) |
已过时。
从 WebRequest 和 SerializationInfo 类的指定实例初始化 StreamingContext 类的新实例。 |
属性
AuthenticationLevel |
获取或设置用于此请求的身份验证和模拟的级别。 |
CachePolicy |
获取或设置此请求的缓存策略。 |
ConnectionGroupName |
当在子类中重写时,获取或设置请求的连接组的名称。 |
ContentLength |
当在子类中被重写时,获取或设置所发送的请求数据的内容长度。 |
ContentType |
当在子类中被重写时,获取或设置所发送的请求数据的内容类型。 |
CreatorInstance |
已过时。
当在子类中重写时,获取从 IWebRequestCreate 类派生的工厂对象,该类用于创建为生成对指定 URI 的请求而实例化的 WebRequest。 |
Credentials |
当在子类中被重写时,获取或设置用于对 Internet 资源请求进行身份验证的网络凭据。 |
DefaultCachePolicy |
获取或设置此请求的默认缓存策略。 |
DefaultWebProxy |
获取或设置全局 HTTP 代理。 |
Headers |
当在子类中被重写时,获取或设置与请求关联的标头名称/值对的集合。 |
ImpersonationLevel |
获取或设置当前请求的模拟级别。 |
Method |
当在子类中被重写时,获取或设置要在此请求中使用的协议方法。 |
PreAuthenticate |
当在子类中被重写时,指示是否对请求进行预身份验证。 |
Proxy |
当在子类中被重写时,获取或设置用于访问此 Internet 资源的网络代理。 |
RequestUri |
当在子类中被重写时,获取与请求关联的 Internet 资源的 URI。 |
Timeout |
获取或设置请求超时之前的时间长度(以毫秒为单位)。 |
UseDefaultCredentials |
当在子代类中重写时,获取或设置一个 Boolean 值,该值控制 DefaultCredentials 是否随请求一起发送。 |
方法
显式接口实现
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
已过时。
当在子代类中重写时,使用序列化 WebRequest 所需要的数据来填充 SerializationInfo 实例。 |