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)

Syntax

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

Parameters

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.

Exceptions

Exception Condition
ArgumentNullException

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

NetworkException

Unable to get the HTTP Request handle for request.

NetworkException

Unable to get the session handle for request.

NetworkException

Unable to set the network requirement.

Remarks

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).

Examples

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("https://www.contoso.com", UriKind.Absolute));

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

private void response_Callback(IAsyncResult asyncResult)
{                      
   HttpWebRequest httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
   try
   {
      WebResponse response = httpWebRequest.EndGetResponse(asyncResult);
   }
   catch (WebException webEx)
   {
      Dispatcher.BeginInvoke(() =>
      {
         MessageBox.Show(webEx.Message);
      });
   }
}        

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Platforms

Windows Phone

See Also

Reference

WebRequestExtensions Class

Microsoft.Phone.Net.NetworkInformation Namespace