信息
您所需的主题如下所示。但此主题未包含在此库中。

WebRequestExtensions.SetNetworkPreference 方法

2013/12/13

设置 Web 请求的首选项,以使用蜂窝或非蜂窝技术。

Namespace:  Microsoft.Phone.Net.NetworkInformation
程序集:  Microsoft.Phone(位于 Microsoft.Phone.dll 中)

public static void SetNetworkPreference(
	this WebRequest request,
	NetworkSelectionCharacteristics preference
)

参数

request
类型: System.Net.WebRequest
要为其设置首选项的 Web 请求。
preference
类型: Microsoft.Phone.Net.NetworkInformation.NetworkSelectionCharacteristics
指定首选技术类型的枚举值之一。

用法说明

在 Visual Basic 和 C# 中,您可以对类型为 WebRequest 的任何对象调用此方法作为实例方法。使用实例方法语法来调用此方法时,忽略第一个参数。

异常条件
ArgumentNullException

requestnull

NetworkException

无法获取 request 的 HTTP Request 句柄。

NetworkException

无法获取 request 的会话句柄。

NetworkException

无法设置网络首选项。

必须在调用 BeginGetResponse 之前调用此方法。否则,将引发 InvalidOperationException。

有关扩展方法的详细信息,请参见扩展方法(C# 编程指南)如何:调用扩展方法 (Visual Basic)

下面的代码示例演示如何使用 SetNetworkPreference 扩展方法。在此示例中,首选的网络接口为 Cellular。如果手机网络网络接口不可用,将使用其他网络接口。在请求回调中调用 GetCurrentNetworkInterface 以确定所使用的网络接口。


private void StartDownload()
{
   HttpWebRequest request;

   // A NetworkException with the error NetworkError.WebRequestAlreadyFinished will
   // be thrown if GetCurrentNetworkInterface is called after the request is complete.
   // The uri string used here is for illustration purposes only. It may complete before
   // you call GetCurrentNetworkInterface. Replace this with a longer-running request if needed.
   request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");
   request.AllowReadStreamBuffering = false;

   // Our preference, for this example, is a cellular network interface. 
   // Call SetNetworkPreference on an HttpWebRequest before calling BeginGetResponse or
   // an InvalidOperationException will be thrown.
   request.SetNetworkPreference(NetworkSelectionCharacteristics.Cellular);
   IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(response_Callback), request);                     
}       

private void response_Callback(IAsyncResult asyncResult)
{                      
   HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

   // Check to see what network interface is being used. 
   // The call to GetCurrentNetworkInterface must be invoked on the UI thread.
   Dispatcher.BeginInvoke(() =>
   {
      try
      {
         NetworkInterfaceInfo ni = request.GetCurrentNetworkInterface();

      // If a cellular interface was available, we should see that here, since that was our
      // preference. Otherwise, we will get a different network interface type, such as Ethernet
      System.Diagnostics.Debug.WriteLine(ni.InterfaceType.ToString());    
      }
      catch (NetworkException networkException)
      {
         // If the HttpWebRequest was not long running, then this exception will be thrown for
         // the GetCurrentNetworkInterface above.
         if (networkException.NetworkErrorCode == NetworkError.WebRequestAlreadyFinished)
         {
            System.Diagnostics.Debug.WriteLine("Cannot call GetCurrentNetworkInterface if the web request is already complete");
         }
      }
   });       
}               
 

Windows Phone OS

受以下版本支持: 8.0, 7.1

Windows Phone

显示: