WebRequest.GetResponse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当在子类中被重写时,返回对 Internet 请求的响应。
public:
virtual System::Net::WebResponse ^ GetResponse();
public virtual System.Net.WebResponse GetResponse ();
abstract member GetResponse : unit -> System.Net.WebResponse
override this.GetResponse : unit -> System.Net.WebResponse
Public Overridable Function GetResponse () As WebResponse
返回
包含对 Internet 请求的响应的 WebResponse。
例外
当未在子类中重写该方法时,试图访问该方法。
示例
以下示例将 Timeout 属性设置为 10000 毫秒。 如果超时期限在可以返回资源之前过期, WebException 则会引发 。
// Create a new WebRequest Object to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );
Console::WriteLine( "\nThe Timeout time of the request before setting is : {0} milliseconds", myWebRequest->Timeout );
// Set the 'Timeout' property in Milliseconds.
myWebRequest->Timeout = 10000;
// This request will throw a WebException if it reaches the timeout limit
// before it is able to fetch the resource.
WebResponse^ myWebResponse = myWebRequest->GetResponse();
// Create a new WebRequest Object to the mentioned URL.
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");
Console.WriteLine("\nThe Timeout time of the request before setting is : {0} milliseconds",myWebRequest.Timeout);
// Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout=10000;
// This request will throw a WebException if it reaches the timeout limit before it is able to fetch the resource.
WebResponse myWebResponse=myWebRequest.GetResponse();
' Create a new WebRequest Object to the mentioned URL.
Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
Console.WriteLine(ControlChars.Cr + "The Timeout time of the request before setting is : {0} milliseconds", myWebRequest.Timeout)
' Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout = 10000
' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
注解
方法 GetResponse 将请求发送到 Internet 资源并返回实例 WebResponse 。 如果请求已由调用 GetRequestStream发起,则 GetResponse 方法将完成请求并返回任何响应。
方法 GetResponse 提供对 的 WebResponse同步访问。 对于异步访问,请使用 BeginGetResponse 和 EndGetResponse 方法。
注意
类 WebRequest 是类 abstract
。 实例在运行时的实际行为 WebRequest 由 方法返回 WebRequest.Create 的后代类确定。 有关默认值和异常的详细信息,请参阅后代类(如 HttpWebRequest 和 FileWebRequest)的文档。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈