2016-07-22 29 views
0

在UWP客戶端,HttpClient的忽略SSL可以使用HttpBaseProtocolFilter,像ODATA上UWP客戶忽略SSL

var filter = new HttpBaseProtocolFilter(); 
filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent; 
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired); 
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted); 
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName); 

Windows.Web.Http.HttpClient webhttpClient = new Windows.Web.Http.HttpClient(filter); 

但是,我怎麼能使用OData的UWP客戶端上,而忽略SSL?

Uri baseUri = new Uri(baseUriStr); 
Container container = new Container(baseUri); 

非常感謝。

回答

0

要消耗UWP應用OData服務,我們可以使用變通方法在這個問題:How to use WCF services in Windows 10 Universal App

  1. 創建的Windows 8.1便攜式類庫,改變目標 的Windows 8.1 enter image description here

  2. 在此PCL中添加服務參考號 enter image description here

  3. 添加參考PCL在UWP應用

  4. 呼叫服務是這樣的:Creating an OData v3 Endpoint with Web API 2

    檢查:

    static void DisplayProduct(ClassLibrary2.ServiceReference1.Product product) 
    { 
        Debug.WriteLine("{0} {1} {2}", product.Name, product.Price, product.Category); 
    } 
    
    // Get an entire entity set. 
    static async void ListAllProducts(ClassLibrary2.ServiceReference1.Container container) 
    { 
        var dsQuery = container.Products; 
    
        var tf = new TaskFactory<IEnumerable<ClassLibrary2.ServiceReference1.Product>>(); 
        var list = (await tf.FromAsync(dsQuery.BeginExecute(null, null), 
               iar => dsQuery.EndExecute(iar))).ToList(); 
        foreach (var p in list) 
        { 
         DisplayProduct(p); 
        } 
    } 
    
    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
        Uri uri = new Uri("http://localhost:18441/odata"); 
        var container = new ClassLibrary2.ServiceReference1.Container(uri); 
    
        ListAllProducts(container); 
    } 
    

我這個文件下創建的測試OData服務我完成的樣品在這裏:Github Link