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. 课下选做作业MyOD

    2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业MyOD 要求 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc ...

  2. Ubuntu 18.04安装Samba服务器及配置

    Ubuntu 18.04安装Samba服务器及配置 局域网下使用samba服务在Linux系统与Windows系统直接共享文件是一项很方便的操作.以Ubuntu为例配置samba服务,Linux服务器 ...

  3. 微信自定义分享 IOS端分享失败

    1.在IOS微信浏览器中自定义分享link 链接中的中文需要encodeURIComponent() 编码(安卓会自动编码) 2.另外在IOS微信浏览器中自定义分享 imgUrl  不能大于34KB ...

  4. thinkphp模版主题使用方法

    3.1.3模版主题使用方法,手册貌似没有.配置项: 'DEFAULT_THEME'=>'default',//默认主题 'THEME_LIST'=>'default,theme',//主题 ...

  5. Response 与 Cookie

    Response 与 Cookie Web服务器要完成的任务的最后一步就是向客户端返回数据.如果客户端是浏览器,那么返回的数据通常是HTML.JS.CSS或者其他类型的代码. Cookie是保存在客户 ...

  6. centos下如何关闭xdebug扩展

    因为只要加载XDEBUG都会导致系统太慢,所以只有在需要调试系统程序的时候才加载XDEBUG扩展.平时一定要把它给注释掉. 只需要在php.ini文件中注释掉xdebug即可. 即: 前面加一个分号即 ...

  7. Unity和Android交互学习

    这段时间学习Unity和Android互调,从基础开始. 网上的教程有很多,浏览了一圈教程,发现目前有两种方式:一种是直接把 .aar文件导入Unity/Plugins/Android文件夹,另一种是 ...

  8. JQuery Validate - 自定义js验证

    (function (window, $) { var validResult = {}; var checkObjs = { /** * 检查输入的一串字符是否全部是数字 * 输入:str Stri ...

  9. 【VS开发】内存映射文件3

    内存映射文件 内存映射文件,是由一个文件到一块内存的映射.Win32提供了允许应用程序把文件映射到一个进程的函数 (CreateFileMapping).内存映射文件与虚拟内存有些类似,通过内存映射文 ...

  10. 性能优化-屏幕常亮与CPU唤醒

    Android在不使用的时候,屏幕在一段时间以后会变暗,再过一段时间就会熄屏,此时CPU就会休眠,那么在这个时候,Timer.Handler.Thread.Service等都会暂停,有时候我们需要屏幕 ...