php httphelper,C#的HttpHelper类post ,get
HeaderList = new Dictionary();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestURL);
request.KeepAlive = true;
HttpWebResponse reponse = null;
Stream stream = null;
string strResponse = string.Empty;
try
{
//URI scheme(抽象标识符体系): 若为"https"则需加载证书并验证
if (request.RequestUri.Scheme == "https")
{
#region 加载证书
//挂接验证服务端证书的回调
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
//获取本地主机名称作为证书查找的参数
string findValue = Dns.GetHostName();
X509Certificate2Collection _certsCollection = store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false);
X509Certificate2 x509c = null;
// MessageBox.Show("证书个数: " + _certsCollection.Count);
//strResponse += "证书个数: " + _certsCollection.Count + "\n\n";
if (_certsCollection.Count > 0)
{
//MessageBox.Show("有证书");
x509c = _certsCollection[0];
request.ClientCertificates.Add(x509c);
}
else
{
//MessageBox.Show("没有证书");
}
#endregion
}
request.Timeout = 30000;
request.UserAgent = DefaultUserAgent;
//request.Referer = "www.baidu.com";
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
request.Headers.GetValues("Location");
reponse = (HttpWebResponse)request.GetResponse();
//foreach (string HeaderKey in reponse.Headers)
//{
// strResponse += HeaderKey + ": " + reponse.Headers[HeaderKey] + "\n";
//}
//strResponse += "\n\n";
//MessageBox.Show("StatusCode = " + reponse.StatusCode);
//strResponse += "StatusCode = " + reponse.StatusCode + "\n\n";
if (reponse.StatusCode == HttpStatusCode.OK)
{
stream = reponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(stream, Encoding.UTF8);
strResponse += myStreamReader.ReadToEnd();
myStreamReader.Close();
stream.Close();
}
}
catch (WebException ex)
{
//MessageBox.Show("异常1----" + ex);
throw new WebException(ex.Message, ex.InnerException);
}
catch (Exception ex)
{
//MessageBox.Show("异常2----" + ex);
throw new Exception(ex.Message, ex.InnerException);
}
finally
{
if (stream != null) { stream.Close(); }
if (reponse != null) { reponse.Close(); reponse = null; }
if (request != null) { request = null; }
}
//如果未收到负载均衡系统返回报文响应,抛出异常信息。
if (string.IsNullOrEmpty(strResponse))
{
throw new Exception("警告:服务请求失败,负载均衡系统无响应,请确认服务请求已正确配置!");
}
//返回信息
return strResponse;
}
public static bool RemoteCertificateValidationCallback(Object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
public static string post(string requestURL, IDictionaryparameters, CookieCollection cookies) {
string strResponse = string.Empty;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestURL);
request.KeepAlive = true;
HttpWebResponse reponse = null;
Stream stream = null;
try
{
//URI scheme(抽象标识符体系): 若为"https"则需加载证书并验证
if (request.RequestUri.Scheme == "https")
{
#region 加载证书
//挂接验证服务端证书的回调
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
//获取本地主机名称作为证书查找的参数
string findValue = Dns.GetHostName();
X509Certificate2Collection _certsCollection = store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false);
X509Certificate2 x509c = null;
// MessageBox.Show("证书个数: " + _certsCollection.Count);
//strResponse += "证书个数: " + _certsCollection.Count + "\n\n";
if (_certsCollection.Count > 0)
{
//MessageBox.Show("有证书");
x509c = _certsCollection[0];
request.ClientCertificates.Add(x509c);
}
else
{
//MessageBox.Show("没有证书");
}
#endregion
}
request.Timeout = 30000;
request.UserAgent = DefaultUserAgent;
//request.Referer = "www.baidu.com";
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
request.Headers.GetValues("Location");
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
//发送POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
i++;
}
}
byte[] data = Encoding.ASCII.GetBytes(buffer.ToString());
using (stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
string[] values = request.Headers.GetValues("Content-Type");
reponse = (HttpWebResponse)request.GetResponse();
if (reponse.StatusCode == HttpStatusCode.OK)
{
StreamReader myStreamReader = new StreamReader(stream, Encoding.UTF8);
strResponse += myStreamReader.ReadToEnd();
myStreamReader.Close();
stream.Close();
}
}
catch (WebException ex)
{
//MessageBox.Show("异常1----" + ex);
throw new WebException(ex.Message, ex.InnerException);
}
catch (Exception ex)
{
//MessageBox.Show("异常2----" + ex);
throw new Exception(ex.Message, ex.InnerException);
}
finally
{
if (stream != null) { stream.Close(); }
if (reponse != null) { reponse.Close(); reponse = null; }
if (request != null) { request = null; }
}
//如果未收到负载均衡系统返回报文响应,抛出异常信息。
if (string.IsNullOrEmpty(strResponse))
{
throw new Exception("警告:服务请求失败,负载均衡系统无响应,请确认服务请求已正确配置!");
}
return strResponse;
}
}
}
php httphelper,C#的HttpHelper类post ,get相关推荐
- C#HttpHelper爬虫类源码分享--苏飞版
介绍 C#HttpHelper实现了C#HttpWebRequest抓取时无视编码,无视证书,无视Cookie,并且实现的代理的功能,使用它您可以进行Get和Post请求,可以很方便 的设置Cooki ...
- [C#HttpHelper]官方产品发布与源码下载---苏飞版
转自:http://www.sufeinet.com/thread-3-1-1.html C#HttpHelper官方产品发布与源码下载---苏飞版 导读部分 -------------------- ...
- 使用苏飞httphelper开发自动更新发布文章程序
最近新上线了一个网站,专门收集网上签到赚钱,有奖活动等等的网站 我就要集分宝 http://www.591jfb.com.新建立 了一个栏目"每日更新",这样就需要每天都登录到网站 ...
- HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理...
原文地址:http://blog.csdn.net/cdefg198/article/details/8315438 万能框架:http://www.sufeinet.com/forum.php?mo ...
- C# HttpHelper帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取...
//取当前webBrowser登录后的Cookie值 [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLa ...
- HttpHelper类
/// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提 ...
- 【转】HttpHelper类登录淘宝联盟并下载淘宝客订单xls
利用飞哥的HttpHelper 1.3版本(因为我用VS2005开发,1.3报错少)实现模拟登录淘宝联盟并且下载淘宝客订单xls文件.现提供源码下载,有不明白的可以问我,同时如果您有好的修改建议请回复 ...
- HttpHelper类登录淘宝联盟并下载淘宝客订单xls c#模拟淘宝登录
c# 利用飞哥的HttpHelper 1.3版本(因为我用VS2005开发,1.3报错少)实现模拟登录淘宝联盟并且下载淘宝客订单xls文件.现提供源码下载,有不明白的可以问我,同时如果您有好的修改建议 ...
- HttpHelper类登录淘宝联盟并下载淘宝客订单xls
利用飞哥的HttpHelper 1.3版本(因为我用VS2005开发,1.3报错少)实现模拟登录淘宝联盟并且下载淘宝客订单xls文件.现提供源码下载,有不明白的可以问我,同时如果您有好的修改建议请回复 ...
最新文章
- HTML5 未来不可阻挡的力量
- 关于vs中代码生成的运行库
- Spring框架bean的注解管理方法之一 使用注解生成对象
- android左右旋转动画效果图,Android新姿势:3D翻转效果原理
- GetGeneratedKeysHelper 与反射
- 高德地图JavaScript API开发研究
- 【牛客 - 315C】排列(思维,贪心,同优则立证明法)
- Windows + Ubuntu下JDK与adb/android环境变量配置完整教程
- JAVA基础知识(四):final关键字
- 如何使用WebSocket
- Android Camera2+HAL3架构
- warning: control may reach end of non-void function [-Wreturn-type]
- html中父子元素的解释,CSS 子绝父相 理解
- wget下载文件命令
- python生成3d人体模型_无限想象空间,用Python就能玩的3D人体姿态估计
- 基于浏览器的3D网页游戏JavaScript 3D游戏引擎介绍
- Java车辆牌照识别
- AI相关英语词汇(持续更新)
- 终结版水与油——致我献给魔术的青春
- mysql安装出现应用程序无法正常启动(oxc000007b)的解决方案
热门文章
- Delphi中的容器类
- 第15章习题解答(二)——《x86汇编语言:从实模式到保护模式》读书笔记41
- 程序的加载和执行(四)——《x86汇编语言:从实模式到保护模式》读书笔记24
- Head First JSP---随笔四
- Hi3516A开发--根文件系统
- Java开发:(执行系统(例cmd)命令)Runtime.getRuntime().exec()参数解释
- 1063 Set Similarity
- php项目列表,php – 如何获取类似项目的列表
- Java未被捕获的异常
- Android工程模块化平台的设计