HttpWebRequest.GetResponse Yöntem

Tanım

İnternet kaynağından bir 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ış, önceki bir çağrısı BeginGetResponse(AsyncCallback, Object)tarafından zaten kullanılıyor.

-veya-

TransferEncoding bir değere ayarlanır ve SendChunked şeklindedir false.

Method GET veya HEAD şeklindedir ve ContentLength sıfırdan büyük veya sıfıra eşit veya SendChunked şeklindedir true.

-veya-

KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is falseve Method is POST veya PUT.

-veya-

HttpWebRequest bir varlık gövdesine sahiptir, ancak GetResponse() yöntemi çağrılmadan çağrılırGetRequestStream().

-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 servis edilebileceğ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 HttpWebResponseşeklindedir ve HTTP'ye özgü özelliklere erişmek için bu sınıfa yazı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 trueayarlar 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 SendChunkedfalse arabelleğe alma devre dışı bırakıldığında ve bir tutma bağlantısında ( KeepAlive özelliği şöyledir true) oluşur:.

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 yapılmaz.

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şturulursa WebException , sunucudan Response yanıtı belirlemek için özel durumun ve Status özelliklerini kullanın.

Not

Uygulamanızda ağ izlemeyi etkinleştirdiğinizde, bu üye izleme bilgilerini çıkarır. Daha fazla bilgi için .NET Framework ağ izleme bölümüne bakın.

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.