HttpClient.GetAsync(Uri) | getAsync(Uri) method

Applies to Windows and Windows Phone

Send a GET request to the specified Uri as an asynchronous operation.

Syntax


httpClient.getAsync(uri).done( /* Your success and error handlers */ );

Parameters

uri

Type: Windows.Foundation.Uri [JavaScript] | System.Uri [.NET] | Windows::Foundation::Uri [C++]

The Uri to which the request is to be sent.

Return value

Type: IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>

The object representing the asynchronous operation.

Exceptions

A number of errors can cause exceptions to occur when calling this method. You must write code to handle exceptions when you call this method. Exceptions can result from parameter validation errors, name resolution failures, and network errors. Exceptions from network errors (loss of connectivity, connection failures, and other HTTP errors, for example) can happen at any time. These errors result in exceptions being thrown. If not handled by your app, an exception can cause your entire app to be terminated by the runtime.

An app can use the HRESULT from the exception to determine the error that caused the exception. An app can then decide how to handle the exception based on the error code. For information on network exceptions and how to retrieve the HRESULT, see Handling exceptions in network apps.

For a list of common errors that result in exceptions, see the Error Codes section below.

If the method fails, the exception is can be one of following common exceptions.

ExceptionCondition
COMException

Thrown when a feature-specific HRESULT is returned from a method call.

This is the most common exception that is thrown by networking methods. An app should use the HRESULT from the exception to determine the cause of the error.

AccessDeniedException

Thrown when access is denied to a resource or feature. This exception occurs when an app doesn't have the required network capabilities set in the app manifest for the network operation requested.

InvalidArgumentException

Thrown when one of the arguments that are provided to a method is not valid.

If user-supplied input caused this exception, an app could inform the user and request new input.

ObjectDisposedException

Thrown when an operation is performed on a disposed object.

OutOfMemoryException

Thrown when insufficient memory is available to complete the operation.

Error codes

An app can use the HRESULT from an exception to learn more detailed information on the error that caused the exception. Possible values for the HRESULT are listed in the Winerror.h header file.

The most common errors that can cause exceptions are below in numeric order based on the HRESULT value.

Name/valueMeaning
COR_E_FILENOTFOUND
0x80070002L

The system cannot find the file specified. The error occurs when using a filter to set the HttpBaseProtocolFilter.HttpCacheReadBehavior to OnlyFromCache and the file was not found in the cache.

ERROR_INVALID_NAME
123L

The syntax used in the uri parameter is incorrect. This error occurs if three slashes are used after the protocol scheme (http:///, for example).

If user-supplied input caused this exception, an app could inform the user and request new input or parse the user input to eliminate the error.

E_ILLEGAL_METHOD_CALL
0x8000000EL

A method was called at an unexpected time. This error occurs if the uri parameter contains a protocol scheme other than http:// or https://.

If user-supplied input caused this exception, an app could inform the user and request new input or parse the user input to eliminate the error.

RO_E_CLOSED
0x80000013L

The object has been closed. This error occurs if the HttpClient or associated filter has been disposed.

E_INVALIDARG
0x80070057L

The uri parameter was a null reference (Nothing in Visual Basic).

WININET_E_NAME_NOT_RESOLVED
0x80072EE7L

The server name or address specified in the uri parameter could not be resolved.

If user-supplied input caused this exception, an app could inform the user and request new input and retry the operation.

WININET_E_CANNOT_CONNECT
0x80072EFDL

A connection with the server specified in the uri parameter could not be established.

If this error occurs , an app might retry the operation. A common approach is to retry a certain number of times with an increasing back-off interval between retries.

If the operation fails, your app may need to plan to continue without network connectivity using previously cached data.

WININET_E_CONNECTION_ABORTED
0x80072EFELL

The connection with the server was terminated abnormally.

If this error occurs , an app might retry the operation. A common approach is to retry a certain number of times with an increasing back-off interval between retries.

If the operation fails, your app may need to plan to continue without network connectivity using previously cached data.

WININET_E_CONNECTION_RESET
80072EFFL

The connection with the server was reset.

If this error occurs , an app might retry the operation again. A common approach is to retry a certain number of times with an increasing back-off interval between retries.

INET_E_RESOURCE_NOT_FOUND
0x800C0005L

The system cannot locate the resource specified in the uri parameter.

If user-supplied input caused this exception, an app could inform the user and request new input and retry the operation.

INET_E_REDIRECT_FAILED
0x800C0014L

A redirection problem occurred.

ERROR_WINHTTP_NOT_REDIRECTED
0x80072F80

A redirection problem occurred.

ERROR_WINHTTP_INCORRECT_PASSWORD
0x80072EEE

An incorrect password was presented to the server.

ERROR_WINHTTP_NO_CM_CONNECTION
0x80072F30

There was a problem connecting to the server.

Remarks

This operation will not block. The returned IAsyncOperationWithProgress(HttpResponseMessage, HttpProgress) object will complete after the whole response (including content) is read.

For sample code in C#/VB/C++ and XAML that shows how to use HttpClient and GetAsync(Uri) to connect to an HTTP server and send a GET request, see Connecting to an HTTP server using Windows.Web.Http.HttpClient (XAML).

For sample code in JavaScript and HTML that shows how to use HttpClient and GetAsync(Uri) to connect to an HTTP server and send a GET request, see Connecting to an HTTP server using Windows.Web.Http(XAML).

Requirements

Minimum supported client

Windows 8.1

Minimum supported server

Windows Server 2012 R2

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.Web.Http
Windows::Web::Http [C++]

Metadata

Windows.winmd

See also

Other resources
Connecting to an HTTP server using Windows.Web.Http.HttpClient (XAML)
Connecting to an HTTP server using Windows.Web.Http(XAML)
Handling exceptions in network apps
Reference
GetAsync(Uri, HttpCompletionOption)
HttpClient
IAsyncOperationWithProgress(HttpResponseMessage, HttpProgress)
System.Uri
Windows.Foundation.Uri
Windows.Web.Http

 

 

Show:
© 2014 Microsoft