static string proxyIpAddress = AppConfig.GetProxyIpAddress;
static string proxyUserName = AppConfig.GetProxyUserName;
static string proxyPassword = AppConfig.GetProxyPassword; static int _timeout = ; #region CUP Method
/// <summary>
/// 请求与响应的超时时间
/// </summary>
static public int Timeout
{
get { return _timeout; }
set { _timeout = value; }
} /// <summary>
/// 执行HTTP POST请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="parameters">请求参数</param>
/// <returns>HTTP响应</returns>
static public string DoPost(string url, IDictionary<string, string> parameters)
{
string result = string.Empty;
try
{
HttpWebRequest req = GetWebRequest(url, "POST"); NetworkCredential proxyCredential = new NetworkCredential();
proxyCredential.UserName = proxyUserName;
proxyCredential.Password = proxyPassword;
req.Credentials = proxyCredential; WebProxy proxy = new WebProxy(proxyIpAddress);
proxy.Credentials = proxyCredential;
req.Proxy = proxy; var httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
};
httpClientHandler.PreAuthenticate = true;
httpClientHandler.UseDefaultCredentials = false;
httpClientHandler.Credentials = proxyCredential;
var client = new HttpClient(handler: httpClientHandler, disposeHandler: true); var res = client.GetStringAsync(url).Result; req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters));
Stream reqStream = req.GetRequestStream();
reqStream.Write(postData, , postData.Length);
reqStream.Close(); HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
result = GetResponseAsString(rsp, encoding);
}
catch (Exception ex)
{
result = string.Format("Request exception:{0}, please try again later.", ex.Message);
}
return result;
} /// <summary>
/// 执行HTTP GET请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="parameters">请求参数</param>
/// <returns>HTTP响应</returns>
static public string DoGet(string url, IDictionary<string, string> parameters)
{
string result = string.Empty;
try
{
if (parameters != null && parameters.Count > )
{
if (url.Contains("?"))
{
url = url + "&" + BuildQuery(parameters);
}
else
{
url = url + "?" + BuildQuery(parameters);
}
} HttpWebRequest req = GetWebRequest(url, "GET");
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
result = GetResponseAsString(rsp, encoding);
}
catch (Exception ex)
{
result = string.Format("Request exception:{0}, please try again later.", ex.Message);
}
return result;
} static public HttpWebRequest GetWebRequest(string url, string method)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.Expect100Continue = false;
req.Method = method;
req.KeepAlive = true;
//req.UserAgent = "Aop4Net";
req.Timeout = _timeout;
return req;
} /// <summary>
/// 把响应流转换为文本。
/// </summary>
/// <param name="rsp">响应流对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>响应文本</returns>
static public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
StringBuilder result = new StringBuilder();
Stream stream = null;
StreamReader reader = null; try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
reader = new StreamReader(stream, encoding); // 按字符读取并写入字符串缓冲
int ch = -;
while ((ch = reader.Read()) > -)
{
// 过滤结束符
char c = (char)ch;
if (c != '\0')
{
result.Append(c);
}
}
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
} return result.ToString();
} /// <summary>
/// 组装普通文本请求参数用于post请求
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
static public string BuildQuery(IDictionary<string, string> parameters)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false; IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
{
if (hasParam)
{
postData.Append("&");
} postData.Append(name);
postData.Append("=");
postData.Append(Uri.EscapeDataString(value));
hasParam = true;
}
} return postData.ToString();
}
#endregion

China Union Pay helper的更多相关文章

  1. 集成 Union Pay - 银联支付

    作者感言 前面已经把WeChat SDK的支付, AliPay SDK搞得七七八八了, 接下来就是银联支付的Union Pay SDK.最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系我, ...

  2. 集成 Union Pay - 银联支付--ios

    请看这个网址,谢谢谢 http://www.cnblogs.com/oc-bowen/p/6000389.html

  3. 银联卡中关于CVN/CVN2/ICVN的区别

    银联China Union Pay,是中国唯一合法的卡组织机构,同时也是EMVCo成员.关于银联卡中CVN/CVN2/ICVN的区别,刚开始我自己不了解,但经过查找资料和请教其他人,对它们的概念也渐渐 ...

  4. 每日英语:Why Are Items Pricier in China?

    In China, consumers pay nearly $1 more for a latte at Starbucks than their U.S. counterparts. A Cadi ...

  5. ApplePay

    ApplePay要在项目有里配置,,配置好项目之后,就剩下编码了,做ApplePay首先要检查设备是否支持ApplePay,支持 ApplePay的设备在 iPhone6及以后,  PKPayment ...

  6. python手机号码运营商归属测试

    #手机号码测试: def number_test(): while True: number = input('Please enter your phone number:') CN_mobile ...

  7. MySQL 常用命令大全2

    下面贴出我在实际工作中遇到mysql操作数据表的sql命令,如有不对的地方,请多指教: c++链接mysql头文件命令 g++ is_in_polygon.cpp -o is_in_polygon - ...

  8. Deploy .Net project automatically with MsBuild and MsDeploy (1)

    Q: How to change parameter values in configuration files dynamically In the first section http://www ...

  9. Python3 循环和判断小练习

    设计一个函数, 在桌面上创建10个文本, 以数字给它们命名 def text_creation(): path = r'C:\Users\Black\Desktop\test\\' for name ...

随机推荐

  1. Linux 配置eth0网卡

    缘由: 最近要复习docker,电脑没有centos7,于是下载了一个最新版的centos7,迫于强迫症没有eth0网卡,为了将ens33改为eth0于是寻求方法. 1.编辑网卡的配置文件 vi /e ...

  2. vue cli 安装element-ui

    1.安装elment-ui --save 参数:上线打包 MacBookPro:vue_test zhangxm$ npm install element-ui axios --save npm WA ...

  3. vi下如何配置自动更新标签(tags)?

    答: 在~/.vimrc中写入以下内容即可: au BufWritePost *.c,*.cpp,*.h silent! !ctags -R & 参考资料: 一键打造vim ide

  4. 安装Oracle11g出现INS-13001环境不满足最低要求

    原版:https://blog.csdn.net/Q_Sea__/article/details/79012808 第一次安装Oracle11g,就出现这个问题,就找了一些解决方案.现在总结一下. 出 ...

  5. 高级java面试宝典

    1.spring事物的配置 spring事物分为俩种,一种是注解事物,一种是AOP事物注解事物的配置: 事物的隔离级别,事物的传播性,事物的超时回滚,哪些异常回滚,哪些不回滚,有默认的回滚规则注解事物 ...

  6. Ubuntu 16.04安装MySQL设置远程访问出现问题的完美解决方案(error:10061)

    一.安装mysql 安装mysql过程中,需要设置mysql的root账号的密码,不要忽略了. sudo apt-get install mysql-server apt isntall mysql- ...

  7. 提取站长之家IP批量查询

    1.工具说明 写报告的时候为了细致性,要把IP地址对应的地区给整理出来.500多条IP地址找出对应地区复制粘贴到报告里整了一个上午. 为了下次更好的完成这项重复性很高的工作,所以写了这个小的脚本. 使 ...

  8. selenium3 web自动化测试框架 二:页面基础操作、元素定位方法封装、页面操作方法封装

    学习目的: 掌握自动化框架中需要的一些基础web操作 正式步骤: 使用title_contains检查页面是否正确 # -*- coding:utf-8 -*- import time from se ...

  9. PJzhang:exiftool图片信息提取工具和短信接口调用工具TBomb

    猫宁!!! 作者:Phil Harvey 这是图片信息提取工具的地址: https://sno.phy.queensu.ca/~phil/exiftool/ 网站隶属于Sudbury 中微子天文台,从 ...

  10. mysqlslap 性能测试

    --create-schema=name 指定测试的数据库名,默认是mysqlslap --engine=name 创建测试表所使用的存储引擎,可指定多个 --concurrency=N 模拟N个客户 ...