最近公司项目需要对接Https接口,将对接的代码整理如下:

 public void Get()
{
HttpWebRequest request = null;
request = WebRequest.Create("https://10.15.67.27:8073/api/tvwall") as HttpWebRequest;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request.ProtocolVersion = HttpVersion.Version11;
// 这里设置了协议类型。
ServicePointManager.SecurityProtocol = (SecurityProtocolType); ;// SecurityProtocolType.Tls1.2;
request.KeepAlive = false;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = ;
ServicePointManager.Expect100Continue = false; request.Method = "GET"; //使用get方式发送数据
request.ContentType = "application/json";
request.Accept = "application/json";
request.Referer = null;
request.AllowAutoRedirect = true; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
string result = string.Empty;
using (StreamReader sr = new StreamReader(stream))
{
result = sr.ReadToEnd();
}
Console.Write(result);
}
 public void Post()
{ HttpWebRequest request = null;
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
request = WebRequest.Create("https://10.15.67.27:8073/api/tvwall/25/scene/new") as HttpWebRequest;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request.ProtocolVersion = HttpVersion.Version11;
// 这里设置了协议类型。
ServicePointManager.SecurityProtocol = (SecurityProtocolType); // SecurityProtocolType.Tls1.2;
request.KeepAlive = false;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = ;
ServicePointManager.Expect100Continue = false; request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
request.Accept = "application/json";
request.Referer = null;
request.AllowAutoRedirect = true;
StringBuilder builder = new StringBuilder(); foreach(var dic in parameters)
{
builder.AppendLine(string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
formDataBoundary,
dic.Key,
dic.Value));
}
builder.AppendLine(string.Format("--{0}--", formDataBoundary)); byte[] formData = encoding.GetBytes(builder.ToString());
Stream newStream = request.GetRequestStream();
newStream.Write(formData, , formData.Length);
newStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
string result = string.Empty;
using (StreamReader sr = new StreamReader(stream))
{
result = sr.ReadToEnd();
}
Console.Write(result);
}

C# 对接Https接口的更多相关文章

  1. [Java] 绕过证书验证调 HTTPS 接口时报 “SSLHandshakeException: DHPublicKey does not comply to algorithm constraints”的解决办法

    作者: zyl910 一.缘由 最近有在对接一个无证书的HTTPS接口时,总是收到"SSLHandshakeException: DHPublicKey does not comply to ...

  2. C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求

    C# 动态创建SQL数据库(二) 使用Entity Framework  创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关 ...

  3. 用Charles抓取https接口数据

    由于我之前抓取的某APP接口全面换上了https接口,导致我在抓取过程中遇到了很大的困境 用Charles无法获取到内容,由于现在已经搞定了,无法展示当时的错误信息,我从网站找了一个类似的错误信息 首 ...

  4. 通过HttpWebRequest请求https接口

    一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...

  5. java实现https,https接口请求

    /**********************https 接口'*******************/ /** * 安全证书管理器 */public class MyX509TrustManager ...

  6. Spring boot 配置https 实现java通过https接口访问

    近来公司需要搭建一个https的服务器来调试接口(服务器用的spring boot框架),刚开始接触就是一顿百度,最后发现互联网认可的https安全链接的证书需要去CA认证机构申请,由于是调试阶段就采 ...

  7. LR访问Https接口

    实操篇 第一步:需要跟开发或者运维要到要访问的https接口的证书(有关证书的问题我们在原理中有解释). 第二步:确定要来的证书的格式是否为pem格式的.首先,LR只能够识别pem格式的证书而且是DE ...

  8. Java调用Http/Https接口(7,end)--WebClient调用Http/Https接口

    WebClient是Spring提供的非阻塞.响应式的Http客户端,提供同步及异步的API,将会代替RestTemplate及AsyncRestTemplate.文中所使用到的软件版本:Java 1 ...

  9. Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口

    RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...

随机推荐

  1. SSH框架整合过程总结

    ---------------------siwuxie095                                 SSH 框架整合过程总结         (一)导入相关 jar 包(共 ...

  2. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  3. Broadcast总结 service

    有时候离开应用就会接收不到系统的广播是因为系统默认发送的广播都会有一个参数 ntent startIntent = new Intent();startIntent.putExtra("pk ...

  4. php 使用PHPExcel 导出数据为Excel

    <?php require_once 'PHPExcel/Classes/PHPExcel.php'; /** * 导出数据为Excel * @param array $fieldArr 标题数 ...

  5. Spring框架的属性注入

    1. 对于类成员变量,常用的注入方式有两种 * 构造函数注入(没有空的构造方法注入) * 属性setter方法注入(有空的构造方法注入) 2. 在Spring框架中提供了前两种的属性注入的方式 1. ...

  6. Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别

    原文链接:http://www.cnblogs.com/signheart/p/922dcf75dd5fe6b418d4475af89c4664.html 使用Thread.currentThread ...

  7. macos修改vmware Fusion的NAT网络

    https://blog.csdn.net/zhishengqianjun/article/details/77046796 http://pubs.vmware.com/fusion-5/index ...

  8. PS大神的作品,每张都是科幻大片!

    相信大家在网上一定见过 各种PS的作品 但是要想成为“PS大神”, 不仅仅要会P图, 最关键的就是脑洞! 同样的马路破坏效果 在大神操作后变成了大片! 摩托车换成了骏马 这效果果然不一般! 这个绝对牛 ...

  9. Vue.js 登录注册实现

    转载 http://www.jb51.net/article/118003.htm

  10. 2018.09.28 hdu5434 Peace small elephant(状压dp+矩阵快速幂)

    传送门 看到n的范围的时候吓了一跳,然后发现可以矩阵快速幂优化. 我们用类似于状压dp的方法构造(1(1(1<<m)∗(1m)*(1m)∗(1<<m)m)m)大小的矩阵. 然后 ...