.NET发送请求(get/post/http/https),携带json数据,接收json数据
C#发送https请求有一点要注意:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
httpRequest.ProtocolVersion = HttpVersion.Version10;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
否则会报错:未能建立安全的SSL/TLS通道
发送请求代码:
- /// <summary>
- /// 发送请求(get/post/http/https)
- /// </summary>
- /// <param name="Uri">请求地址</param>
- /// <param name="JsonStr">json数据</param>
- /// <param name="Method">请求方式POST/GET</param>
- /// <returns></returns>
- public static string ClientRequest(string Uri, string JsonStr, string Method = "POST")
- {
- try
- {
- var httpRequest = (HttpWebRequest)HttpWebRequest.Create(Uri);
- httpRequest.Method = Method;
- httpRequest.ContentType = "application/json";
- if (Method.ToLower() == "get")
- {
- httpRequest.ContentType = "application/x-www-form-urlencoded";
- }
- httpRequest.Proxy = null;
- httpRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
- httpRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.8,zh-hk;q=0.6,ja;q=0.4,zh;q=0.2");
- httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
- //如果是发送HTTPS请求
- if (Uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
- {
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
- httpRequest.ProtocolVersion = HttpVersion.Version10;
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
- }
- else
- {
- ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
- }
- if (!string.IsNullOrEmpty(JsonStr))
- {
- using (var dataStream = new StreamWriter(httpRequest.GetRequestStream()))
- {
- dataStream.Write(JsonStr);
- dataStream.Flush();
- dataStream.Close();
- }
- }
- var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
- using (var dataStream = new StreamReader(httpResponse.GetResponseStream()))
- {
- var result = dataStream.ReadToEnd();
- return result;
- }
- }
- catch (Exception ex)
- {
- return "{\"error\":\"" + ex.Message + "\"}";
- }
- }
- private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- return true; //总是接受
- }
//接收示例
Response.ContentType = "application/json";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
using (var reader = new System.IO.StreamReader(Request.InputStream))
{
string xmlData = reader.ReadToEnd();
if (!string.IsNullOrEmpty(xmlData))
{
//业务处理
JavaScriptSerializer jss = new JavaScriptSerializer();
PushModel model = jss.Deserialize(xmlData, typeof(PushModel)) as PushModel;
if (model != null)
{
channel_ids = model.channel_id;
msg = model.msg;
}
}
}
http://blog.csdn.net/lsm135/article/details/50367315
.NET发送请求(get/post/http/https),携带json数据,接收json数据的更多相关文章
- [转]php中 curl模拟post发送json并接收json
本文转自:https://blog.csdn.net/pangchengyong0724/article/details/52103962 本地模拟请求服务器数据,请求数据格式为json,服务器返回数 ...
- stm32 usb数据接收与数据发送程序流程分析
http://blog.csdn.net/u011318735/article/details/17424349 既然学习了USB,那就必须的搞懂USB设备与USB主机数据是怎么通讯的.这里主要讲设备 ...
- php中 curl模拟post发送json并接收json(转)
本地模拟请求服务器数据,请求数据格式为json,服务器返回数据也是json. 由于需求特殊性, 如同步客户端的批量数据至云端, 提交至服务器的数据可能是多维数组数据了. 这时需要将此数据以一定的数据 ...
- 记录一次排查使用HttpWebRequest发送请求的发生“基础连接已关闭:接收时发生错误”异常问题的过程
描述:某次更新程序,需要给测试员MM测试,之前都是正常的,更新后给MM测试就报异常System.Net.WebException 基础连接已经关闭:接收时发生错误 -------> System ...
- 爬虫模块介绍--request(发送请求模块)
爬虫:可见即可爬 # 每个网站都有爬虫协议 基础爬虫需要使用到的三个模块 requests 模块 # 模拟发请求的模块 PS:python原来有两个模块urllib和urllib的升级urlli ...
- Ajax发送请求的四个步骤
1.创建XMLHttpRequest let xhr=new XMLHttpRequest; 2.连接服务器 xhr.open("get","goods.json&quo ...
- RestTemplate发送请求并携带header信息
1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...
- RestTemplate发送请求并携带header信息 RestTemplate post json格式带header信息
原文地址: http://www.cnblogs.com/hujunzheng/p/6018505.html RestTemplate发送请求并携带header信息 v1.使用restTempl ...
- 发送请求时携带了参数,但是浏览器network没有显示的排错思路
发送请求时携带了参数,但是浏览器network没有显示的排错思路 不知道大家有没有遇到这样子的情况就是发送请求的时候明明携带了参数,但是浏览器的network中就是没有!请看下图和代码! 我发送请求用 ...
随机推荐
- 【洛谷】P1357 花园(状压+矩阵快速幂)
题目 传送门:QWQ 分析 因为m很小,考虑把所有状态压成m位二进制数. 那么总状态数小于$ 2^5 $. 如果状态$ i $能转移到$ j $,那么扔进一个矩阵,n次方快速幂一下. 答案是对角线之和 ...
- android 系统架构简介
Android系统采取的是分层的架构,根据官方文档提供的架构图,我们将android的系统架构分成5层,如图: 1.Application Framework (应用框架) application f ...
- html大小写问题
js中变量名,函数,关键字都区分大小写,如var i;和var I;是两个不同的变量. css中定义的元素名称不区分大小写的. html中,标签和标签属性统一使用小写形式,固有属性也一律使用小写,自定 ...
- JavaScript字符串练习
题目: 预备代码: // 自定义输出 var log = function () { console.log.apply(this, arguments); }; // ====== // 测试 // ...
- opencv查看源代码
这一节是一个插曲,有的人刚开始学opencv就看源代码,有的人直接拿着opencv的API用...... 学了一个多月opencv了,就是没找到源代码,想看的时候都是从网上找的,或者看网上说从哪个文件 ...
- IPv4报文分片
1:为什么需要分片 每个数据链路层协议都有自己的帧格式,在这个格式中有一个字段是"数据字段最大长度"(MTU,最大传输单元),当数据报被封装成帧时,数据报的总长度必须小于这个最大长 ...
- WPF 颜色拾色器
效果图: 下载:Code 参考: http://www.codeproject.com/Articles/33001/WPF-A-Simple-Color-Picker-With-Previewhtt ...
- zabbix修改中文乱码
参考网站; https://blog.csdn.net/open_data/article/details/47447029 字体下载网站: http://www.font5.com.cn/zitix ...
- 命令查询职责分离(CQRS)模式
参考: http://www.cnblogs.com/yangecnu/p/Introduction-CQRS.html
- JAVA 整合 SSM (Spring + SpringMVC + MyBatis)
< 一 > POM 配置文件 ( 如果出现 JAR 包 引入错误, 请自行下载 ) <project xmlns="http://maven.apache.org/POM/ ...