C# HTTP请求对外接口、第三方接口公用类
/// <summary>
/// 网络数据请求公共函数
/// </summary>
public class HttpWebRequestCommon
{ #region 根据HTTP协议请求接口,不携带参数
/// <summary>
/// 根据HTTP协议请求接口,不携带参数
/// </summary>
/// <param name="url">请求的第三方接口地址</param>
/// <returns>接口返回的数据</returns>
public static string HttpRequest(string url)
{
//string returnData = null;
string ret = string.Empty;
try
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
webReq.Method = "POST";
webReq.ContentType = "application/json";
Stream postData = webReq.GetRequestStream();
postData.Close();
HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
}
catch (Exception ex)
{
//LogManager.LogInstance.WriteLog("时间:" + DateTime.Now + "/n 请求出错原因" + ex.ToString());
}
return ret;
}
#endregion #region 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数。传递格式请参照下个函数
/// <summary>
/// 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数
/// </summary>
/// <param name="PostUrl">请求的第三方地址</param>
/// <param name="postData">在地址后拼接的参数集合,传递格式请参照调用示范</param>
/// <returns>接口返回的数据格式</returns>
public static string HttpRequestUrl(string PostUrl, byte[] postData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length; Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postData, 0, postData.Length);
newStream.Flush();
newStream.Close();
string msg = string.Empty;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
if (myResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); msg = reader.ReadToEnd();
}
return msg;
}
#endregion #region 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数。传递格式请参照下个函数
/// <summary>
/// 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数
/// </summary>
/// <param name="PostUrl">请求的第三方地址</param>
/// <param name="postData">在地址后拼接的参数集合,传递格式请参照调用示范</param>
/// <returns>接口返回的数据格式</returns>
public static string HttpRequestUrl(string PostUrl, byte[] postData, string POSTisGET)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
myRequest.Method = POSTisGET;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length; Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postData, 0, postData.Length);
newStream.Flush();
newStream.Close();
string msg = string.Empty;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
if (myResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); msg = reader.ReadToEnd();
}
return msg;
}
#endregion #region 上面HttpRequestUrl函数的调用示范,url存请求地址。参数通过字符串占位符拼接
//string url = "https://api.weixin.qq.com/cgi-bin/token"; //string postStrTpl = "grant_type=client_credential&appid={0}&secret={1}"; //UTF8Encoding encoding = new UTF8Encoding();
//byte[] postData = encoding.GetBytes(string.Format(postStrTpl, "参数一", "参数二")); //string retString = HttpWebRequestCommon.RequestUrl(url, postData);
#endregion #region 根据HTTP协议请求第三方接口,携带XML参数
/// <summary>
/// 根据HTTP协议请求第三方接口,携带XML参数
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="data">携带的XML数据</param>
/// <returns>接口返回的数据</returns>
public static string HttpRequestXml(string url, string data)
{
//string returnData = null;
string ret = string.Empty;
try
{
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(data);
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = buffer.Length;
Stream postData = webReq.GetRequestStream();
postData.Write(buffer, 0, buffer.Length);
postData.Close();
HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
}
catch (Exception ex)
{
//LogManager.LogInstance.WriteLog("时间:" + DateTime.Now + "/n 请求出错原因" + ex.ToString());
}
return ret;
}
#endregion #region 根据HTTP协议请求第三方接口,携带Json参数
/// <summary>
/// 根据HTTP协议请求第三方接口,携带Json参数。
/// </summary>
/// <param name="url">请求的第三方地址</param>
/// <param name="content">Post提交数据内容(utf-8编码的)</param>
/// <returns>接口返回的数据</returns>
public static string HttpRequestJson(string url, string content)
{
string result = "";
//ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json"; #region 添加Post 参数
byte[] data = Encoding.UTF8.GetBytes(content);
req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
#endregion }
C# HTTP请求对外接口、第三方接口公用类的更多相关文章
- java代码调用第三方接口
一.利用httpclient来字符串参数(url是第三方接口,不带参数,如:http://192.168.16.200:8081/faceInfo/list,param是url后面所要带的参数) pu ...
- PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)
开发者端:发送请求,并接收结果 <?php // 下面的demo,实现的功能如下: // 1-开发者需要判断一个用户是否存在,去请求第三方接口. // 2-与第三方接口的通信,是以xml格式传送 ...
- 分别使用http,express,koa请求第三方接口
今天又再次恶补了一下http的内容,确切地说是node.js里面的http的内容,啊,百度了半天express怎么请求第三方接口,结果发现自己买的入门书籍都有这个内容.舍近求远,我真是醉了.还有百度上 ...
- VUE 使用axios请求第三方接口数据跨域问题解决
VUE是基于node.js,所以解决跨域问题,设置一下反向代理即可. 我这里要调用的第三方接口地址为 http://v.juhe.cn/toutiao/index?type=top&key=1 ...
- Java Web系统经常使用的第三方接口
1. Web Service 接口 1.1 接口方式说明和长处 在笔者的开发生涯中,当作为接口提供商给第三方提供接口时,以及作为client去调用第三方提供的接口时,大部分时候都是使用 Web Se ...
- Java Web系统常用的第三方接口
1. Web Service 接口 1.1 接口方式说明和优点 在笔者的开发生涯中,当作为接口提供商给第三方提供接口时,以及作为客户端去调用第三方提供的接口时,大部分时候都是使用 Web Se ...
- sdk开发时,对外暴露的接口封装
思考,用同步还是异步? 实质就是屏蔽一些东西,让使用者直接传参数 拿结果 而不用关心具体实现 eg.登陆接口 1.定义接口LoginCallBack,两个函数 请求成功和失败 public inter ...
- 根据ip地址从第三方接口获取详细的地理位置
最近项目某个功能需要根据ip地址从第三方接口获取详细的地理位置,从网上找了很多例子,主要接口有新浪的,淘宝的,腾讯的.试了淘宝的,如果是数量级小的还可以,如果数量级达到上十万级就速度慢了,会导致系统崩 ...
- 调用支付宝第三方接口(沙箱环境) SpringMVC+Maven
一.蚂蚁金服开放平台的操作 网址:https://open.alipay.com/platform/home.htm 支付宝扫码登陆
随机推荐
- 在Visual Studio 中使用git——使用git管理源代码(三)
在Visual Studio 中使用git--什么是Git(一) 在Visual Studio 中使用git--给Visual Studio安装 git插件(二) 第三部分:使用git管理源代码 ...
- mysql 密码忘记解决办法
bin>net stop mysql bin>mysqld --skip-grant-tables bin>mysql mysql>use mysql mysql>upd ...
- Linux配置yum源(本地源和网络源)
目录 一:配置本地yum源 二:配置网络yum源 更新源可以获取最新的软件信息,以更新您的系统 Redhat7配置源 YUM(Yellow dog Updater Modified): yum是Re ...
- IPC$共享和其他共享(C$、D$)
目录 net use共享命令的用法 IPC$ IPC空连接 ipc$使用的端口 关闭IPC$共享 net use共享命令的用法 net use #查看连接 net share ...
- Python中Scapy网络嗅探模块的使用
目录 Scapy scapy的安装和使用 发包 发包和收包 抓包 将抓取到的数据包保存 查看抓取到的数据包 格式化输出 过滤抓包 Scapy scapy是python中一个可用于网络嗅探的非常强大的第 ...
- Bettercap2.X版本的使用
目录 Bettercap 安装 ARP欺骗 DNS 欺骗 注入脚本 结合Beef-XSS 替换下载文件 Bettercap 很多人应该都听过或者用过Ettercap,这是Kali下一款优秀的ARP欺骗 ...
- Windows PR提权
目录 提权利用的漏洞 PR提权 提权利用的漏洞 Microsoft Windows RPCSS服务隔离本地权限提升漏洞 RPCSS服务没有正确地隔离 NetworkService 或 LocalSer ...
- 内网域渗透之MS14-068复现
在做域渗透测试时,当我们拿到了一个普通域成员的账号后,想继续对该域进行渗透,拿到域控服务器权限.如果域控服务器存在MS14_068漏洞,并且未打补丁,那么我们就可以利用MS14_068快速获得域控服务 ...
- 一起来看看java并发中volatile关键字的神奇之处
并发编程中的三个概念: 1.原子性 在Java中,对基本数据类型的变量的读取和赋值操作是原子性操作,即这些操作是不可被中断的,要么执行,要么不执行. 2.可见性 对于可见性,Java提供了volati ...
- javaScript的成长之路【何为函数,面向对象又是啥!!!】