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. 6.13-C3p0连接池配置,DBUtils使用

    DBCP连接池 一.C3p0连接池配置 开源的JDBC连接池 使用连接池的好处: 减轻数据库服务器压力 数据源: ComboPooledDataSource ComboPooledDataSource ...

  2. C# AtomicLong

    using System; using System.Threading; /// <summary> /// Provides lock-free atomic read/write u ...

  3. delphi WebBrowser的使用方法详解(三)

    WebBrowser 操作记要 WebBrowser1.GoHome;  //到浏览器默认主页 WebBrowser1.Refresh;  //刷新 WebBrowser1.GoBack;  //后退 ...

  4. mysql更新(五) 完整性约束 外键的变种 三种关系 数据的增删改

    11-数据的增删改   本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识. ...

  5. jmeter测试20个QPS下的响应时间-设置QPS限制

    添加--->定时器--->Constant Throughput Timer Constant Throughput Timer 的主要属性介绍: 名称:定时器的名称 Target thr ...

  6. JS修改属性,六种数据类型

    JS修改属性 一般修改单个属性是通过JS修改的,比较方便.改多个属性通过css样式改更方便. 1.特殊:通过JS修改包含"-"符号的属性,例如margin-top // 特殊 修改 ...

  7. 《GPU高性能编程CUDA实战》第七章 纹理内存

    ▶ 本章介绍了纹理内存的使用,并给出了热传导的两个个例子.分别使用了一维和二维纹理单元. ● 热传导(使用一维纹理) #include <stdio.h> #include "c ...

  8. 开发者必看|Android 8.0 新特性及开发指南

    背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Andro ...

  9. Linux批量查询替换字符串

    Linux 批量查询替换文本文件中的字符串: 1.批量查找某个目下文件的包含的内容,例如: #   grep -rn "要找查找的文本" ./ 2.批量查找并替换文件内容. #   ...

  10. iTunes 错误 -50

    iTunes,给苹果安装软件,这个软件的体验这么差!!! 手机上基本打不开AppStore,用电脑iTunes,经常莫名其妙的错误代码冒出. 速度奇慢无比. error -50 打开iTunes -- ...