C# 对接Https接口
最近公司项目需要对接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接口的更多相关文章
- [Java] 绕过证书验证调 HTTPS 接口时报 “SSLHandshakeException: DHPublicKey does not comply to algorithm constraints”的解决办法
作者: zyl910 一.缘由 最近有在对接一个无证书的HTTPS接口时,总是收到"SSLHandshakeException: DHPublicKey does not comply to ...
- C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求
C# 动态创建SQL数据库(二) 使用Entity Framework 创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关 ...
- 用Charles抓取https接口数据
由于我之前抓取的某APP接口全面换上了https接口,导致我在抓取过程中遇到了很大的困境 用Charles无法获取到内容,由于现在已经搞定了,无法展示当时的错误信息,我从网站找了一个类似的错误信息 首 ...
- 通过HttpWebRequest请求https接口
一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...
- java实现https,https接口请求
/**********************https 接口'*******************/ /** * 安全证书管理器 */public class MyX509TrustManager ...
- Spring boot 配置https 实现java通过https接口访问
近来公司需要搭建一个https的服务器来调试接口(服务器用的spring boot框架),刚开始接触就是一顿百度,最后发现互联网认可的https安全链接的证书需要去CA认证机构申请,由于是调试阶段就采 ...
- LR访问Https接口
实操篇 第一步:需要跟开发或者运维要到要访问的https接口的证书(有关证书的问题我们在原理中有解释). 第二步:确定要来的证书的格式是否为pem格式的.首先,LR只能够识别pem格式的证书而且是DE ...
- Java调用Http/Https接口(7,end)--WebClient调用Http/Https接口
WebClient是Spring提供的非阻塞.响应式的Http客户端,提供同步及异步的API,将会代替RestTemplate及AsyncRestTemplate.文中所使用到的软件版本:Java 1 ...
- Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口
RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...
随机推荐
- collections之deque【双向队列】与Queue【单向队列】
今天来向大家介绍两个队列,一个是deque,双向队列,另外一个是Queue,单向队列,队列和堆栈不同,队列为先进先出,大家还需要注意一下,双向队列为collections模块中的类,而Queue为qu ...
- Spring框架的事务管理之声明式事务管理的类型
1. 声明式事务管理又分成两种方式 * 基于AspectJ的XML方式(重点掌握)(具体内容见“https://www.cnblogs.com/wyhluckdog/p/10137712.html”) ...
- 移动端 输入框 如果类型是number,用户也可以输入汉字和字母
<input type="number" id="input-age" placeholder="请输入你的年龄" /> //i ...
- PM2使用及介绍
pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的.它非常适合IaaS结构,但不要把它用于 ...
- mysql水平分区
解决问题:单表数据量过大 ALTER TABLE boc_url_log PARTITION BY RANGE (ulid) ( PARTITION log_1 VALUES LESS THAN () ...
- vc到vs2015消息函数
afx_msg LRESULT OnMyIconNotify(WPARAM wParam,LPARAM lParam); vc6 可以是void vs2015不可以 ON_MESSAGE(MYWM_ ...
- Vue.js 2.0 跨域请求数据
Vuejs由1.0更新到了2.0版本.HTTP请求官方也从推荐使用Vue-Resoure变为了 axios .接下来我们来简单地用axios进行一下异步请求.(阅读本文作者默认读者具有使用npm命令的 ...
- centos 6.5 安装mysql
步骤1: yum -y install mysql-server 步骤2: chkconfig mysqld on 步骤3: service mysqld start mysql -u root se ...
- R 语言安装问题集锦
R安装问题R CMD INSTALL -l /data1/jhh/envirment/R/R_lib /data1/jhh/software/qvalue_2.10.0.tar.gz 问题1 :con ...
- 2018软工项目UML设计(团队)
团队信息 队名:火箭少男100 本次作业课上成员 短学号 名 本次作业博客链接 2507 俞辛(临时队长) https://www.cnblogs.com/multhree/p/9821080.htm ...