WebRequestExtensions.SetNetworkRequirement Method

July 28, 2014

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)

public static void SetNetworkRequirement(
	this WebRequest request,
	NetworkSelectionCharacteristics requirement
)

Parameters

request
Type: System.Net.WebRequest
The web request for which to set the requirement.
requirement
Type: Microsoft.Phone.Net.NetworkInformation.NetworkSelectionCharacteristics
One of the enumeration values that specify the required type of technology.

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.

ExceptionCondition
ArgumentNullException

request is null.

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.

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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2014 Microsoft