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数据的更多相关文章

  1. [转]php中 curl模拟post发送json并接收json

    本文转自:https://blog.csdn.net/pangchengyong0724/article/details/52103962 本地模拟请求服务器数据,请求数据格式为json,服务器返回数 ...

  2. stm32 usb数据接收与数据发送程序流程分析

    http://blog.csdn.net/u011318735/article/details/17424349 既然学习了USB,那就必须的搞懂USB设备与USB主机数据是怎么通讯的.这里主要讲设备 ...

  3. php中 curl模拟post发送json并接收json(转)

    本地模拟请求服务器数据,请求数据格式为json,服务器返回数据也是json. 由于需求特殊性, 如同步客户端的批量数据至云端, 提交至服务器的数据可能是多维数组数据了.  这时需要将此数据以一定的数据 ...

  4. 记录一次排查使用HttpWebRequest发送请求的发生“基础连接已关闭:接收时发生错误”异常问题的过程

    描述:某次更新程序,需要给测试员MM测试,之前都是正常的,更新后给MM测试就报异常System.Net.WebException 基础连接已经关闭:接收时发生错误 -------> System ...

  5. 爬虫模块介绍--request(发送请求模块)

    爬虫:可见即可爬   # 每个网站都有爬虫协议 基础爬虫需要使用到的三个模块 requests 模块  # 模拟发请求的模块 PS:python原来有两个模块urllib和urllib的升级urlli ...

  6. Ajax发送请求的四个步骤

    1.创建XMLHttpRequest let xhr=new XMLHttpRequest; 2.连接服务器 xhr.open("get","goods.json&quo ...

  7. RestTemplate发送请求并携带header信息

    1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...

  8. RestTemplate发送请求并携带header信息 RestTemplate post json格式带header信息

    原文地址:  http://www.cnblogs.com/hujunzheng/p/6018505.html RestTemplate发送请求并携带header信息   v1.使用restTempl ...

  9. 发送请求时携带了参数,但是浏览器network没有显示的排错思路

    发送请求时携带了参数,但是浏览器network没有显示的排错思路 不知道大家有没有遇到这样子的情况就是发送请求的时候明明携带了参数,但是浏览器的network中就是没有!请看下图和代码! 我发送请求用 ...

随机推荐

  1. 常用模块:re ,shelve与xml模块

    一 shelve模块: shelve模块比pickle模块简单,只有一个open函数,所以使用完之后要使用f.close关闭文件.返回类似字典的对象,可读可写;key必须为字符串,而值可以是pytho ...

  2. 通过yum升级gcc/g++至版本4.8.2

    最近在坐一个日期处理的问题,需要安装sxtwl模块,但是gcc版本4.7死活也安装不上,最后测试到只能升级gcc版本到4.8才得以解决 [root@123 bin]# gcc -vUsing buil ...

  3. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...

  4. flask中的蓝图与红图

    内容: 1.flask中的蓝图 2.flask子域名实现 3.flask中的红图 1.flask中的蓝图 一个大型项目中视图比较多,如果仅仅是写在app.py中不方便管理,蓝图就可以做到分功能分目录结 ...

  5. tornado-通过判断后台数据限制登陆--简单的

    import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供 ...

  6. 关于有些邮件可以在http上发送成功但是https不能发送成功一个思路方法

    关于有些邮件可以在http上发送成功但是https不能发送成功 其实如果是单纯的发送邮件,是没问题 今天一个客户出现这个问题,进行排查 他的邮件发送是任务制的, 是通过CURL请求的, 我估计她的CU ...

  7. yii 执行sql

    sql         $sql = "SELECT ".join(',', $this->search_fields_channel)." FROM {{chan ...

  8. Spring cron 定时调度配置

    IDEA 或者 STS http://spring.io/guides/gs/scheduling-tasks/ spring  mvc : 结构: Seconds Minutes Hours Day ...

  9. Hibernate常用网址

    Hibernate 官网用户手册: http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide ...

  10. springBoot异常处理

    1.status=404 Whitelabel Error Page Whitelabel Error Page This application has no explicit mapping fo ...