HttpWebRequest.GetResponse Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
bir İnternet kaynağından yanıt döndürür.
public:
override System::Net::WebResponse ^ GetResponse();
public override System.Net.WebResponse GetResponse ();
override this.GetResponse : unit -> System.Net.WebResponse
Public Overrides Function GetResponse () As WebResponse
Döndürülenler
WebResponse İnternet kaynağından gelen yanıtı içeren bir.
Özel durumlar
Akış, öğesine yapılan önceki bir çağrı BeginGetResponse(AsyncCallback, Object)tarafından zaten kullanılıyor.
-veya-
TransferEncoding bir değere ayarlanır ve SendChunked değeridir false
.
Method GET veya HEAD şeklindedir ve ContentLength sıfırdan büyük veya sıfıra eşit ya da SendChunked olur true
.
-veya-
KeepAlive is true
, AllowWriteStreamBuffering is false
, ContentLength is -1, SendChunked is false
ve Method is POST veya PUT.
-veya-
bir HttpWebRequest varlık gövdesine sahiptir, ancak GetResponse() yöntemi yöntemi çağrılmadan çağrılır GetRequestStream() .
-veya-
ContentLength sıfırdan büyüktür, ancak uygulama taahhüt edilen verilerin tümünü yazmaz.
İstek önbelleği doğrulayıcı, bu istek için yanıtın önbellekten verilebileceğini belirtti; ancak, bu istek sunucuya gönderilecek verileri içerir. Veri gönderen istekler önbelleği kullanmamalıdır. Bu özel durum, yanlış uygulanan bir özel önbellek doğrulayıcı kullanıyorsanız oluşabilir.
Abort() daha önce çağrıldı.
-veya-
İsteğin zaman aşımı süresi doldu.
-veya-
İstek işlenirken bir hata oluştu.
Örnekler
Aşağıdaki kod örneği bir isteğin yanıtını alır.
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;
// Specify the URL to receive the request.
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(args[1]));
// Set some reasonable limits on resources used by this request
request->MaximumAutomaticRedirections = 4;
request->MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request->Credentials = CredentialCache::DefaultCredentials;
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
Console::WriteLine("Content length is {0}", response->ContentLength);
Console::WriteLine("Content type is {0}", response->ContentType);
// Get the stream associated with the response.
Stream^ receiveStream = response->GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader^ readStream = gcnew StreamReader(receiveStream, Encoding::UTF8);
Console::WriteLine("Response stream received.");
Console::WriteLine(readStream->ReadToEnd());
response->Close();
readStream->Close();
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
using System;
using System.Net;
using System.Text;
using System.IO;
public class Test
{
// Specify the URL to receive the request.
public static void Main (string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
Console.WriteLine(readStream.ReadToEnd());
response.Close();
readStream.Close();
}
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Test
' Specify the URL to receive the request.
Public Shared Sub Main(ByVal args() As String)
Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4
' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Console.WriteLine("Content length is {0}", response.ContentLength)
Console.WriteLine("Content type is {0}", response.ContentType)
' Get the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()
' Pipes the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Console.WriteLine("Response stream received.")
Console.WriteLine(readStream.ReadToEnd())
response.Close()
readStream.Close()
End Sub
End Class
'
'The output from this example will vary depending on the value passed into Main
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'...
'
'
Açıklamalar
yöntemi, GetResponse İnternet kaynağından gelen yanıtı içeren bir WebResponse nesne döndürür. Döndürülen gerçek örnek bir HttpWebResponsedeğeridir ve HTTP'ye özgü özelliklere erişmek için bu sınıfa tür yayını yapılabilir.
sınıfında ayarlanan HttpWebRequest özellikler çakıştığında birkaç durumda A ProtocolViolationException oluşturulur. Bu özel durum, bir uygulama özelliğini ve SendChunked özelliğini olarak true
ayarlar ContentLength ve ardından bir HTTP GET isteği gönderirse oluşur. Bu özel durum, bir uygulama yalnızca HTTP 1.0 protokollerini destekleyen ve bunun desteklenmediği bir sunucuya öbekli göndermeye çalışırsa oluşur. Bu özel durum, bir uygulama özelliği ayarlamadan ContentLength veri göndermeye çalışırsa veya false
SendChunked arabelleğe alma devre dışı bırakıldığında ve bir tutma bağlantısında olduğunda oluşur (KeepAliveözellik )true
.
Dikkat
Akışı kapatmak ve bağlantıyı serbest bırakmak için yöntemini çağırmanız Close gerekir. Bunun yapılmaması, uygulamanızın bağlantılarının dolmasına neden olabilir.
POST yöntemini kullanırken istek akışını almanız, gönderilecek verileri yazmanız ve akışı kapatmanız gerekir. Bu yöntem, içeriğin göndermesini beklemeyi engeller; Zaman aşımı kümesi yoksa ve içerik sağlamazsanız, çağıran iş parçacığı süresiz olarak engeller.
Not
Aynı yanıt nesnesini döndürmek için GetResponse birden çok çağrı yapılır; istek yeniden verilmez.
Not
Uygulamanız belirli bir istek için zaman uyumlu ve zaman uyumsuz yöntemleri karıştıramaz. yöntemini çağırırsanız GetRequestStream , yanıtı almak için yöntemini kullanmanız GetResponse gerekir.
Not
oluşturulursaWebException, sunucudan yanıtı belirlemek için özel durumun ve Status özelliklerini kullanınResponse.
Not
Uygulamanızda ağ izlemeyi etkinleştirdiğinizde, bu üye izleme bilgilerini çıkarır. Daha fazla bilgi için bkz. .NET Framework'te Ağ İzleme.
Not
Güvenlik nedeniyle tanımlama bilgileri varsayılan olarak devre dışı bırakılır. Tanımlama bilgilerini kullanmak istiyorsanız, tanımlama bilgilerini etkinleştirmek için özelliğini kullanın CookieContainer .
Şunlara uygulanır
Ayrıca bkz.
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin