UWP客户端上的Odata忽略ssl

时间:2016-07-22 02:55:15

标签: windows ssl odata uwp

在UWP客户端上,HttpClient忽略ssl可以使用HttpBaseProtocolFilter,比如

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

但是,我如何在uwp客户端上使用odata并忽略ssl?

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

非常感谢。

1 个答案:

答案 0 :(得分:0)

要在UWP应用中使用OData服务,我们可以使用此问题中的解决方法:How to use WCF services in Windows 10 Universal App

  1. 创建 Windows 8.1 Portable类库并将Target更改为 Windows 8.1 enter image description here

  2. 在此PCL中添加服务参考 enter image description here

  3. 在UWP应用程序中添加对PCL的引用

  4. 像这样呼叫服务:

    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);
    }
    
  5. 我按照以下文档创建了测试OData服务:Creating an OData v3 Endpoint with Web API 2

    在此处查看我已完成的示例: Github Link