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.
Assembly: Microsoft.Phone (in Microsoft.Phone.dll)
'Declaration <ExtensionAttribute> _ Public Shared Sub SetNetworkRequirement ( _ request As WebRequest, _ requirement As NetworkSelectionCharacteristics _ )
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.| Exception | Condition |
|---|---|
| ArgumentNullException | request is Nothing. |
| 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); }); } }