WebRequestExtensions.SetNetworkRequirement Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Sets the requirement for a web request to use either cellular or non-cellular technology.

Namespace: Microsoft.Phone.Net.NetworkInformation
Assembly: Microsoft.Phone (in Microsoft.Phone.dll)


<ExtensionAttribute> _
Public Shared Sub SetNetworkRequirement ( _
    request As WebRequest, _
    requirement As NetworkSelectionCharacteristics _
public static void SetNetworkRequirement(
    this WebRequest request,
    NetworkSelectionCharacteristics requirement


Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type WebRequest. When you use instance method syntax to call this method, omit the first parameter.


Exception Condition

request is nullNothingnullptra null reference (Nothing in Visual Basic).


Unable to get the HTTP Request handle for request.


Unable to get the session handle for request.


Unable to set the network requirement.


This method must be called before calling BeginGetResponse. Otherwise, an InvalidOperationException will be thrown.

For more information about extension methods, see Extension Methods (C# Programming Guide) or How to: Call an Extension Method (Visual Basic).


The following code example shows how to use the SetNetworkRequirement extension method. In this example, the network interface that is required is Cellular. If a cellular network interface is not available, a WebException exception will be thrown in the callback of the BeginGetResponse method which is response_Callback in this example. The Message property of the WebException contains the text “The remote server returned an error: NotFound.”.

private void StartDownload()
   HttpWebRequest hwr;
   hwr = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://www.contoso.com", UriKind.Absolute));

   // Call SetNetworkRequirement on an HttpWebRequest before calling BeginGetResponse or
   // an InvalidOperationException will be thrown.
   hwr.BeginGetResponse(new AsyncCallback(response_Callback), hwr);                     

private void response_Callback(IAsyncResult asyncResult)
   HttpWebRequest httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
      WebResponse response = httpWebRequest.EndGetResponse(asyncResult);
   catch (WebException webEx)
      Dispatcher.BeginInvoke(() =>

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1


Windows Phone

See Also


WebRequestExtensions Class

Microsoft.Phone.Net.NetworkInformation Namespace