/// <summary>
/// Get请求
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public static string HttpGet(string Url, string postDataStr)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd(); myStreamReader.Close();
myResponseStream.Close(); if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
} return retString;
}
catch (Exception ex)
{
return "";
} } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // Always accept
Console.WriteLine("accept " + certificate.GetName());
return true; //总是接受
}
 /// <summary>
/// post请求 string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:\"test089\",Name:\"test1\"}");
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string HttpPost(string url, string body)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json"; byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
catch (Exception ex)
{
return "";
}
} /// <summary>
/// post请求 string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:\"test089\",Name:\"test1\"}");
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string HttpPut(string url, string body)
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Put";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json"; byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
} /// <summary>
/// post请求 string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:\"test089\",Name:\"test1\"}");
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string HttpDelete(string url, string body)
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Delete";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json"; byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}

  

  

C# 请求Https的更多相关文章

  1. [转]在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效

    该文原网址:http://www.cnblogs.com/xwgli/p/5487930.html 在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效.   当访问 h ...

  2. 通过HttpWebRequest请求https接口

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

  3. PHP curl请求https遇到的坑

    PHP里curl对https的证书配置默认是服务器端要求验证的,如果服务器端没有配置证书验证,则无法请求https路径.如果为了简便使用不需要配置https证书的话,配置curl时将以下两项设置为fa ...

  4. PHP使用file_get_contents或curl请求https的域名内容为空或Http 505错误的问题排查方法

    前段日子,突然接到用户的反馈,说系统中原来的QQ登录.微博登录通通都不能用,跟踪代码进去后发现,是在 file_get_contents这个函数请求QQ登录的地方报错,在用该函数file_get_co ...

  5. cxf webservice请求https

    本地java请求https接口,不需要添加证书: 只需要修改配置文件applicationContext-soap-client.xml: <beans xmlns="http://w ...

  6. 通过证书请求Https站点

    前几天在做与平安银行对接接口,主要是给平安银行推送用户数据(申请贷款的用户),平安银行提供的是https的地址,请求https地址的时候还要发送证书,刚接到这个任务的时候一头雾水,百度上各种所搜,最后 ...

  7. ASIHttpRequest请求HTTPS

    一种方法 ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:[NSURLURLWithString:bodyString]]; [requ ...

  8. PHP:CURL分别以GET、POST方式请求HTTPS协议接口api

    1.curl以GET方式请求https协议接口 //注意:这里的$url已经包含参数了,不带参数你自己处理哦GET很简单 function curl_get_https($url){ $curl = ...

  9. Android 实现 HttpClient 请求Https

    如题,默认下,HttpClient是不能请求Https的,需要自己获取 private static final int SET_CONNECTION_TIMEOUT = 5 * 1000; priv ...

  10. file_get_contents无法请求https连接的解决方法 php开启curl

    file_get_contents无法请求https连接的解决方法 方法1: PHP.ini默认配置下,用file_get_contents读取https的链接,就会如下错误: Warning: fo ...

随机推荐

  1. django系列8.3.2--django中间件实现登录验证(2) 个人构想逻辑

    middleware.py from django.utils.deprecation import MiddlewareMixin from django.shortcuts import rend ...

  2. kali linux之无线渗透(续)

    Airolib 设计用于存储ESSID和密码列表,计算生成不变的PMK(计算资源消耗型) PMK在破解阶段被用于计算PTK(速度快,计算资源要求少) 通过完整性摘要值破解密码SQLite3数据库存储数 ...

  3. 官宣,PyTorch 1.0 稳定版本现已推出

    简评:快来一起快乐地学习吧. 随着 PyTorch 生态系统和社区继续为开发人员提供有趣的新项目和教育资源,今天(12 月 7日)在 NeurIPS 会议上发布了 PyTorch 1.0 稳定版.研究 ...

  4. XCode7无证书真机调试教程

    转载自http://altair21.com/156.html 前提条件: XCode版本>=7 1. 进入xcode,菜单栏选择xcode –> preferences (快捷键 com ...

  5. postman小结

    1.get和post请求,get有限制2k,post没有限制post安全 在选择的时候别把get post选错然后,run 2.data 选成txt文件  utf-8 ip ip,result12.1 ...

  6. 基于iTop4412的FM收音机系统设计(二)

    说明:第一版架构为:APP+JNI(NDK)+Driver(linux),优点是开发简单,周期短,也作为自己的毕业设计 现在更新第二版,FM服务完全植入Android系统中,成为系统服务,架构为:AP ...

  7. windows server 2012 valid key

    好吧,网页三剑客. 1, load disc iso 2,check ip settings, 3,net-inst-server-start 4,power Node, F2 4.1 F7 usbc ...

  8. 新创建的数据库,执行db2look时,遇到package db2lkfun.bnd bind failed

    在新创建的数据库中,执行db2look的时候,存在这样的问题 db2v97i1@oc0644314035 ~]$ db2look -d sample -e -l -o db2look.ddl -- N ...

  9. 使用EditPlus编辑Linux上的文本文件

    在Linux上我们都使用vim 或者vi命令对文件进行编辑,但是我们习惯的一般都是windows系统, 那么怎么才能像在windows上一样编辑我们Linux上的文件呢?下面我们就来看看如何使用 wi ...

  10. PHP之mb_stripos使用

    mb_stripos (PHP 5 >= 5.2.0, PHP 7) mb_stripos - Finds position of first occurrence of a string wi ...