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;
    

      

  • 相关阅读:
    3.for in循环
    2.break与continue
    1.XHTML框架结构
    lamda表达式在EF中的应用
    View数据呈现相关技术
    ASP.NET MVC 4 技术讲解
    ASP.NET MVC 相关的社群与讨论区
    C# 随机红包算法
    圆圈里带 小写字母,大写字母
    使用SQL语句 检测 MSSQL死锁
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5133357.html
Copyright © 2011-2022 走看看