HttpWebRequest.GetResponse Method (System.Net) - MSDN - Microsoft [PDF]

System_CAPS_note Note. Your application cannot mix synchronous and asynchronous methods for a particular request. If you

4 downloads 19 Views 180KB Size

Recommend Stories


MSDN-AA Available Software List
Forget safety. Live where you fear to live. Destroy your reputation. Be notorious. Rumi

[PDF] Troubleshooting Microsoft Technologies
Never let your sense of morals prevent you from doing what is right. Isaac Asimov

PDF Microsoft Office 2007
Learning never exhausts the mind. Leonardo da Vinci

microsoft project - WordPress.com [PDF]
will demonstrate this capability using our “Order Processing System” project from Example 9.1. ...... Use your imagination and assume that a CPM/PERT network, as shown in the figure at the top of the following page, with the following estimated a

Visual Studio 2013 with MSDN 에디션
Love only grows by sharing. You can only have more for yourself by giving it away to others. Brian

[PDF] Programming Microsoft ASP.NET MVC
Forget safety. Live where you fear to live. Destroy your reputation. Be notorious. Rumi

Barrierefreie PDF aus Microsoft Word
Suffering is a gift. In it is hidden mercy. Rumi

Microsoft Word to PDF Guide
Those who bring sunshine to the lives of others cannot keep it from themselves. J. M. Barrie

[PDF] Exploring Microsoft SharePoint 2013
Don't count the days, make the days count. Muhammad Ali

[PDF] Download Microsoft Excel 2013
You have survived, EVERY SINGLE bad day so far. Anonymous

Idea Transcript


> System.Net > HttpWebRequest Class > HttpWebRequest Methods

HttpWebRequest.GetResponse Method ()

Abort Method AddRange Method

.NET Framework (current version)

Other Versions

BeginGetRequestStream Method BeginGetResponse Method EndGetRequestStream Method EndGetResponse Method GetObjectData Method GetRequestStream Method GetResponse Method MemberwiseClone Method ISerializable.GetObjectData Method Note The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns a response from an Internet resource. Namespace: System.Net Assembly: System (in System.dll)

Syntax C#

C++

F#

VB

public override WebResponse GetResponse()

Return Value Type: System.Net.WebResponse A WebResponse that contains the response from the Internet resource.

Exceptions Exception

Condition

InvalidOperationException

The stream is already in use by a previous call to BeginGetResponse. -orTransferEncoding is set to a value and SendChunked is false.

ProtocolViolationException

Method is GET or HEAD, and either ContentLength is greater or equal to zero or SendChunked is true. -orKeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT. -orThe HttpWebRequest has an entity body but the GetResponse method is called without calling the GetRequestStream method. -orThe ContentLength is greater than zero, but the application does not write all of the promised data.

NotSupportedException

The request cache validator indicated that the response for this request can be served from the cache; however, this request includes data to be sent to the server. Requests that send data must not use the cache. This exception can occur if you are using a custom cache validator that is incorrectly implemented.

WebException

Abort was previously called. -orThe time-out period for the request expired. -orAn error occurred while processing the request.

Remarks The GetResponse method returns a WebResponse object that contains the response from the Internet resource. The actual instance returned is an HttpWebResponse, and can be typecast to that class to access HTTP-specific properties. A ProtocolViolationException is thrown in several cases when the properties set on the HttpWebRequest class are conflicting. This exception occurs if an application sets the ContentLength property and the SendChunked property to true, and then sends an HTTP GET request. This exception occurs if an application tries to send chunked to a server that only supports HTTP 1.0 protocol, where this is not supported. This exception occurs if an application tries to send data without setting the ContentLength property or the SendChunked is false when buffering is disabled and on a keepalive connection (the KeepAlive property is true).

Caution You must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.

When using the POST method, you must get the request stream, write the data to be posted, and close the stream. This method blocks waiting for content to post; if there is no time-out set and you do not provide content, the calling thread blocks indefinitely.

Note Multiple calls to GetResponse return the same response object; the request is not reissued.

Note Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

Note If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

Note This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note For security reasons, cookies are disabled by default. If you wish to use cookies, use the CookieContainer property to enable cookies.

Examples The following code example gets the response for a request. C#

C++

VB

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. ... */

Version Information .NET Framework Available since 1.1

See Also Timeout HttpWebRequest Class System.Net Namespace Element (Network Settings)

Return to top Print Share

IN THIS ARTICLE Syntax Exceptions Remarks Examples Version Information See Also

Is this page helpful?

Dev centers Windows Office



Yes



No

Learning resources

Community

Support

Microsoft Virtual Academy

Forums

Self support

Channel 9

Blogs

MSDN Magazine

Codeplex

Visual Studio Microsoft Azure More...

United States (English)

Programs BizSpark (for startups) Microsoft Imagine (for students)

Newsletter

Privacy & cookies

Terms of use

Trademarks

© 2018 Microsoft

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.