zoukankan      html  css  js  c++  java
  • utilize HttpClient to generate a SSL access and generate REST access to fetch data, async programming? cool and brief

    WebRequestHandler handler = new WebRequestHandler();
                    try
                    {
                        X509Certificate2 certificate = new X509Certificate2(System.IO.File.ReadAllBytes(ConfigurationManager.AppSettings["webapicertpath"]), ConfigurationManager.AppSettings["webapicertpwd"]); ;
                        handler.ClientCertificates.Add(certificate);
                        ServicePointManager.ServerCertificateValidationCallback =
                            (object sender, X509Certificate certificate1, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
                            {
                                return true;
                            };
                    }
                    catch
                    {
                    }
                    httpClient = new HttpClient(handler);
    

      to fetch data with REST httpclient, just utilize code below:

                    case "get":
                        result = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
                        break;
                    case "post":
                        result = httpClient.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                        break;
                    case "put":
                        result = httpClient.PutAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                        break;
                    case "delete":
                        result = httpClient.DeleteAsync(url).Result.Content.ReadAsStringAsync().Result;
                        break;
                    default:
                        break;
    

      

  • 相关阅读:
    BZOJ 2006 [NOI2010]超级钢琴
    标准打印一棵树
    COJ 0288 路径(2015升级版)
    批判树套树。。。。。。。。
    BestCoder Round #49
    蓝牙通信中读取固定长度数组的解决
    5月5日的规划
    必须要改变这样的生活
    五一结束
    五一来临
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5133357.html
Copyright © 2011-2022 走看看