C#中HttpWebRequest的用法详解
http://www.jb51.net/article/57156.htm这篇文章主要介绍了C#中HttpWebRequest的用法,以实例的形式详细叙述了HttpWebRequest类中GET与POST的用法,非常具有参考借鉴价值,需要的朋友可以参考下
HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来对数据进行获取和提交。下面对这两种方式进行一下说明:
GET 方式:
GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.jb51.net/?hl=zh-CN 中,前面部分 http://www.jb51.net表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。
程序代码如下:
复制代码 代码如下:HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.jb51.net?hl=zh-CN" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}
使用 GET 方式提交中文数据。
GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种。
用 gb2312 方式编码访问的程序代码如下:
复制代码 代码如下:Encoding myEncoding = Encoding.GetEncoding("gb2312");
string address = "http://www.jb51.net/?" + HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}
在上面的程序代码中,我们以 GET 方式访问了网址 http://www.jb51.net ,传递了参数“参数一=值一”,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。
POST 方式:
POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。
程序代码如下:
复制代码 代码如下:string param = "hl=zh-CN&newwindow=1";
byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.jb51.net/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}
在上面的代码中,我们访问了 http://www.jb51.net 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。
使用 POST 方式提交中文数据
POST 方式通过在页面内容中填写参数的方法来完成数据的提交,由于提交的参数中可以说明使用的编码方式,所以理论上能获得更大的兼容性。
用 gb2312 方式编码访问的程序代码如下:
复制代码 代码如下:Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding) + "&" + HttpUtility.UrlEncode("参数二", myEncoding) + "=" + HttpUtility.UrlEncode("值二", myEncoding);
byte[] postBytes = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.jb51.net/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}
从上面的代码可以看出, POST 中文数据的时候,先使用 UrlEncode 方法将中文字符转换为编码后的 ASCII 码,然后提交到服务器,提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。
用C#语言写的关于HttpWebRequest 类的使用方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace HttpWeb
{
/// <summary>
/// Http操作类
/// </summary>
public static class httptest
{
/// <summary>
/// 获取网址HTML
/// </summary>
/// <param name="URL">网址 </param>
/// <returns> </returns>
public static string GetHtml(string URL)
{
WebRequest wrt;
wrt = WebRequest.Create(URL);
wrt.Credentials = CredentialCache.DefaultCredentials;
WebResponse wrp;
wrp = wrt.GetResponse();
string reader = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
try
{
wrt.GetResponse().Close();
}
catch (WebException ex)
{
throw ex;
}
return reader;
}
/// <summary>
/// 获取网站cookie
/// </summary>
/// <param name="URL">网址 </param>
/// <param name="cookie">cookie </param>
/// <returns> </returns>
public static string GetHtml(string URL, out string cookie)
{
WebRequest wrt;
wrt = WebRequest.Create(URL);
wrt.Credentials = CredentialCache.DefaultCredentials;
WebResponse wrp;
wrp = wrt.GetResponse();
string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
try
{
wrt.GetResponse().Close();
}
catch (WebException ex)
{
throw ex;
}
cookie = wrp.Headers.Get("Set-Cookie");
return html;
}
public static string GetHtml(string URL, string postData, string cookie, out string header, string server)
{
return GetHtml(server, URL, postData, cookie, out header);
}
public static string GetHtml(string server, string URL, string postData, string cookie, out string header)
{
byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData);
return GetHtml(server, URL, byteRequest, cookie, out header);
}
public static string GetHtml(string server, string URL, byte[] byteRequest, string cookie, out string header)
{
byte[] bytes = GetHtmlByBytes(server, URL, byteRequest, cookie, out header);
Stream getStream = new MemoryStream(bytes);
StreamReader streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
string getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
return getString;
}
/// <summary>
/// Post模式浏览
/// </summary>
/// <param name="server">服务器地址 </param>
/// <param name="URL">网址 </param>
/// <param name="byteRequest">流 </param>
/// <param name="cookie">cookie </param>
/// <param name="header">句柄 </param>
/// <returns> </returns>
public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
{
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(server), cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Referer = server;
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
contentLength = webResponse.ContentLength;
byte[] outBytes = new byte[contentLength];
outBytes = ReadFully(getStream);
getStream.Close();
return outBytes;
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[128];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
/// <summary>
/// Get模式
/// </summary>
/// <param name="URL">网址 </param>
/// <param name="cookie">cookies </param>
/// <param name="header">句柄 </param>
/// <param name="server">服务器 </param>
/// <param name="val">服务器 </param>
/// <returns> </returns>
public static string GetHtml(string URL, string cookie, out string header, string server)
{
return GetHtml(URL, cookie, out header, server, "");
}
/// <summary>
/// Get模式浏览
/// </summary>
/// <param name="URL">Get网址 </param>
/// <param name="cookie">cookie </param>
/// <param name="header">句柄 </param>
/// <param name="server">服务器地址 </param>
/// <param name="val"> </param>
/// <returns> </returns>
public static string GetHtml(string URL, string cookie, out string header, string server, string val)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
string getString = "";
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.Accept = "*/*";
httpWebRequest.Referer = server;
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(server), cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "GET";
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
return getString;
}
}
}
========
C# 利用HttpListener监听处理Http请求
http://blog.sina.com.cn/s/blog_6f3ff2c90101wwh5.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Xml.Serialization;
using System.Threading;
using System.Web;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
try
{
HttpListener listerner = new HttpListener();
{
for (; true; )
{
try
{
Console.Write("请输入服务器IP地址:");
string ip = Console.ReadLine();
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://" + ip + ":1500/AnsweringMachineService/");
// listerner.Prefixes.Add("http://localhost/web/");
listerner.Start();
}
catch (Exception e)
{
Console.WriteLine("未能成功连接服务器.....");
listerner = new HttpListener();
continue;
}
break;
}
Console.WriteLine("服务器启动成功.......");
int maxThreadNum, portThreadNum;
//线程池
int minThreadNum;
ThreadPool.GetMaxThreads(out maxThreadNum, out portThreadNum);
ThreadPool.GetMinThreads(out minThreadNum, out portThreadNum);
Console.WriteLine("最大线程数:{0}", maxThreadNum);
Console.WriteLine("最小空闲线程数:{0}", minThreadNum);
//ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), x);
Console.WriteLine("\n\n等待客户连接中。。。。");
while (true)
{
//等待请求连接
//没有请求则GetContext处于阻塞状态
HttpListenerContext ctx = listerner.GetContext();
ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc), ctx);
}
con.Close();
listerner.Stop();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write("Press any key to continue . . . ");
Console.ReadKey( );
}
}
static void TaskProc(object o)
{
HttpListenerContext ctx = (HttpListenerContext)o;
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
string type = ctx.Request.QueryString["type"];
string userId = ctx.Request.QueryString["userId"];
string password = ctx.Request.QueryString["password"];
string filename = Path.GetFileName(ctx.Request.RawUrl);
string userName = HttpUtility.ParseQueryString(filename).Get("userName");//避免中文乱码
//进行处理
//使用Writer输出http响应代码
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
writer.Write(“处理结果”);
writer.Close();
ctx.Response.Close();
}
}
}
}
Android客户端:
public static void Register(final Handler handler, final Context context,
final String userId, final String userName,final int groupId, final String password){
new Thread(new Runnable(){
public void run() {
if(!CommonTools.checkNetwork(context)){
Message msg = new Message();
msg.what = Signal.NETWORK_ERR;
handler.sendMessage(msg);
return;
}
try {
String content = "";
String tmp = java.net.URLEncoder.encode(userName, "utf-8"); //防止中文乱码
URL url = new URL(URL+"?type=Register&userId="+userId+"&password="+password+"&groupId="+groupId+"&userName="+tmp);
// HttpURLConnection
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(), "utf-8");
int i;
// read
while ((i = isr.read()) != -1) {
content = content + (char) i;
}
isr.close();
}
//disconnect
httpconn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}//run
}).start();//thread
}
注意:
1.中文乱码问题
在客户端采用如下形式
String tmp = java.net.URLEncoder.encode(userName, "utf-8"); //防止中文乱码
服务器端
string filename = Path.GetFileName(ctx.Request.RawUrl);
string userName = HttpUtility.ParseQueryString(filename).Get("userName");//避免中文乱码
服务器端需要引入: using System.Web;
此时可能提示找不到库,则在项目右键添加引用 找到 System.Web.dll勾选即可
2.[System.Net.HttpListenerException] = {"拒绝访问。"}问题
如果是win7或者win8,在cmd.exe上右键,以管理员身份运行,然后执行下面的命令
netsh http add urlacl url=http://本机IP:1500/ user=用户名(如Administrator)
3.记得关闭防火墙,或者只开放指定端口,步骤如下:
step1、点击控制面板
step2、选择windows防火墙,点击高级设置
step3、在弹出的“高级安全windows防火墙”点击“入站规则”,在右侧“操作”栏点击“入站规则”下的“新建规则…”,此时会弹出一个窗口让你设置。剩下的就非常傻瓜化了。
step4、弹出“新建入站规则向导”-规则类型-选中“端口”,点击下一步。选择规则应用的协议“TCP/UDP”如果是TCP你就选择TCP,UDP就选择UDP。再勾选“特定本地端口”在文本框输入您想开放的端口号(例如1521)。
step5、点击下一步,到“连接符合指定条件时应该进行什么操作?”选择“允许连接”。点击下一步到“配置文件”何时应用该规则,勾选“域”、“专用”、“公用”点击下一步。
step6、配置规则名称,随便输入你自己认为好记的规则名称即可。
========
C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)
http://www.tuicool.com/articles/rmiqYz主题 HttpComponents C#
一、 System.Net.Http .HttpClient简介
System.Net.Http 是微软.net4.5中推出的HTTP 应用程序的编程接口, 微软称之为“现代化的 HTTP 编程接口”, 主要提供如下内容:
1. 用户通过 HTTP 使用现代化的 Web Service 的客户端组件;
2. 能够同时在客户端与服务端同时使用的 HTTP 组件(比如处理 HTTP 标头和消息), 为客户端和服务端提供一致的编程模型。
个人看来是抄袭 apache http client ,目前网上用的人好像不多, 个人认为使用httpclient最大的好处是:不用自己管理cookie,只要负责写好请求即可。
由于网上资料不多,这里借登录博客园网站做个简单的总结其get和post请求的用法。
查看微软的api可以发现其属性方法: http://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.aspx
由其api可以看出如果想 设置请求头 只需要在 DefaultRequestHeaders 里进行设置
创建httpcliet可以直接new HttpClient()
发送请求 可以按发送方式分别调用其方法,如 get调用 GetAsync(Uri) , post调用 PostAsync(Uri, HttpContent) ,其它依此类推。。。
二、实例(模拟post登录博客园)
首先,需要说明的是, 本实例环境是win7 64位+vs 2013+ .net 4.5框架。
1.使用vs2013新建一个控制台程序,或者窗体程序,如下图所示:
2.必须引入System.Net.Http框架,否则将不能使用httpclient
3.实现代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
private static String dir = @"C:\work\";
/// <summary>
/// 写文件到本地
/// </summary>
/// <param name="fileName"></param>
/// <param name="html"></param>
public static void Write(string fileName, string html)
{
try
{
FileStream fs = new FileStream(dir + fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(html);
sw.Close();
fs.Close();
}catch(Exception ex){
Console.WriteLine(ex.StackTrace);
}
}
/// <summary>
/// 写文件到本地
/// </summary>
/// <param name="fileName"></param>
/// <param name="html"></param>
public static void Write(string fileName, byte[] html)
{
try
{
File.WriteAllBytes(dir + fileName, html);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
/// <summary>
/// 登录博客园
/// </summary>
public static void LoginCnblogs()
{
HttpClient httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = 256000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
String url = "http://passport.cnblogs.com/login.aspx";
HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
String result = response.Content.ReadAsStringAsync().Result;
String username = "hi_amos";
String password = "密码";
do
{
String __EVENTVALIDATION = new Regex("id=\"__EVENTVALIDATION\" value=\"(.*?)\"").Match(result).Groups[1].Value;
String __VIEWSTATE = new Regex("id=\"__VIEWSTATE\" value=\"(.*?)\"").Match(result).Groups[1].Value;
String LBD_VCID_c_login_logincaptcha = new Regex("id=\"LBD_VCID_c_login_logincaptcha\" value=\"(.*?)\"").Match(result).Groups[1].Value;
//图片验证码
url = "http://passport.cnblogs.com" + new Regex("id=\"c_login_logincaptcha_CaptchaImage\" src=\"(.*?)\"").Match(result).Groups[1].Value;
response = httpClient.GetAsync(new Uri(url)).Result;
Write("amosli.png", response.Content.ReadAsByteArrayAsync().Result);
Console.WriteLine("输入图片验证码:");
String imgCode = "wupve";//验证码写到本地了,需要手动填写
imgCode = Console.ReadLine();
//开始登录
url = "http://passport.cnblogs.com/login.aspx";
List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
paramList.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
paramList.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
paramList.Add(new KeyValuePair<string, string>("__VIEWSTATE", __VIEWSTATE));
paramList.Add(new KeyValuePair<string, string>("__EVENTVALIDATION", __EVENTVALIDATION));
paramList.Add(new KeyValuePair<string, string>("tbUserName", username));
paramList.Add(new KeyValuePair<string, string>("tbPassword", password));
paramList.Add(new KeyValuePair<string, string>("LBD_VCID_c_login_logincaptcha", LBD_VCID_c_login_logincaptcha));
paramList.Add(new KeyValuePair<string, string>("LBD_BackWorkaround_c_login_logincaptcha", "1"));
paramList.Add(new KeyValuePair<string, string>("CaptchaCodeTextBox", imgCode));
paramList.Add(new KeyValuePair<string, string>("btnLogin", "登 录"));
paramList.Add(new KeyValuePair<string, string>("txtReturnUrl", "http://home.cnblogs.com/"));
response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result;
result = response.Content.ReadAsStringAsync().Result;
Write("myCnblogs.html",result);
} while (result.Contains("验证码错误,麻烦您重新输入"));
Console.WriteLine("登录成功!");
//用完要记得释放
httpClient.Dispose();
}
public static void Main()
{
LoginCnblogs();
}
}
代码分析:
首先,从Main函数开始,调用LoginCnblogs方法;
其次,使用GET方法:
HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
String result = response.Content.ReadAsStringAsync().Result;
再者,使用POST方法:
List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
paramList.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
....
response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result;
result = response.Content.ReadAsStringAsync().Result;
最后, 注意其返回值可以是string,也可以是byte[],和stream的方式,这里看你需要什么吧。
4.登录成功后的截图
1).使用浏览器登录后的截图:
2).使用Httpcliet登录后的截图:
总结,可以发现C#中HttpClient的用法和Java中非常相似,所以,说其抄袭确实不为过。
========
在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
http://zhoufoxcn.blog.51cto.com/792419/561934/这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全部采用手工操作工作量大而且繁琐,因此周公决定利用C#来实现。在很多地方都需要验证用户身份才可以进行下一步操作,这就免不了POST请求来登录,在实际过程中发现有些网站登录是HTTPS形式的,在解决过程中遇到了一些小问题,现在跟大家分享。
通用辅助类
下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebResponse对象,有了返回的HttpWebResponse实例,可以获取登录过程中返回的会话信息,也可以获取响应流。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.DirectoryServices.Protocols;
using System.ServiceModel.Security;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
/*
* 作者:周公(zhoufoxcn)
* 日期:2011-05-08
* 原文出处:http://blog.csdn.net/zhoufoxcn 或http://zhoufoxcn.blog.51cto.com
* 版权说明:本文可以在保留原文出处的情况下使用于非商业用途,周公对此不作任何担保或承诺。
* */
namespace BaiduCang
{
/// <summary>
/// 有关HTTP请求的辅助类
/// </summary>
public class HttpWebResponseUtility
{
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
/// <summary>
/// 创建GET方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static HttpWebResponse CreateGetHttpResponse(string url,int? timeout, string userAgent,CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
return request.GetResponse() as HttpWebResponse;
}
/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static HttpWebResponse CreatePostHttpResponse(string url,IDictionary<string,string> parameters,int? timeout, string userAgent,Encoding requestEncoding,CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if(requestEncoding==null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request=null;
//如果是发送HTTPS请求
if(url.StartsWith("https",StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion=HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
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 = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
}
}
从上面的代码中可以看出POST数据到HTTP和HTTPS站点不同,POST数据到HTTPS站点的时候需要设置ServicePointManager类的ServerCertificateValidationCallback属性,并且在POST到https://passport.baidu.com/?login时还需要将HttpWebResquest实例的ProtocolVersion属性设置为HttpVersion.Version10(这个未验证是否所有的HTTPS站点都需要设置),否则在调用GetResponse()方法时会抛出“基础连接已经关闭: 连接被意外关闭。”的异常。
用法举例
这个类用起来也很简单:
(1)POST数据到HTTPS站点,用它来登录百度:
string loginUrl = "https://passport.baidu.com/?login";
string userName = "userName";
string password = "password";
string tagUrl = "http://cang.baidu.com/"+userName+"/tags";
Encoding encoding = Encoding.GetEncoding("gb2312");
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("tpl", "fa");
parameters.Add("tpl_reg", "fa");
parameters.Add("u", tagUrl);
parameters.Add("psp_tt", "0");
parameters.Add("username", userName);
parameters.Add("password", password);
parameters.Add("mem_pass", "1");
HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, encoding, null);
string cookieString = response.Headers["Set-Cookie"];
(2)发送GET请求到HTTP站点
在cookieString中包含了服务器端返回的会话信息数据,从中提取了之后可以设置Cookie下次登录时带上这个Cookie就可以以认证用户的信息,假设我们已经登录成功并且获取了Cookie,那么发送GET请求的代码如下:
string userName = "userName";
string tagUrl = "http://cang.baidu.com/"+userName+"/tags";
CookieCollection cookies = new CookieCollection();//如何从response.Headers["Set-Cookie"];中获取并设置CookieCollection的代码略
response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies);
(3)发送POST请求到HTTP站点
以登录51CTO为例:
string loginUrl = "http://home.51cto.com/index.php?s=/Index/doLogin";
string userName = "userName";
string password = "password";
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("email", userName);
parameters.Add("passwd", password);
HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null);
在这里说句题外话,CSDN的登录处理是由http://passport.csdn.net/ajax/accounthandler.ashx这个Handler来处理的。
总结
在本文只是讲解了在C#中发送请求到HTTP和HTTPS的用法,分GET/POST两种方式,为减少一些繁琐和机械的编码,周公将其封装为一个类,发送数据之后返回HttpWebResponse对象实例,利用这个实例我们可以获取服务器端返回的Cookie以便用认证用户的身份继续发送请求,或者读取服务器端响应的内容,不过在读取响应内容时要注意响应格式和编码,本来在这个类中还有读取HTML和WML内容的方法(包括服务器使用压缩方式传输的数据),但限于篇幅和其它方面的原因,此处省略掉了。如有机会,在以后的文章中会继续讲述这方面的内容。
========
http协议post数据标准格式
http://blog.163.com/wu_ernan/blog/static/2102880202014226102528240/在C#中有HttpWebRequest类,可以很方便用来获取http请求,但是这个类对Post方式没有提供一个很方便的方法来获取数据。网上有很多人提供了解决方法,但都参差不齐,这里我把我使用的方法总结出来,与大家分享。
本文精华:实现了post的时候即可以有字符串的key-value,还可以带文件。
Post数据格式
Post提交数据的时候最重要就是把Key-Value的数据放到http请求流中,而HttpWebRequest没有提供一个属性之类的东西可以让我们自由添加Key-Value,因此就必须手工构造这个数据。
根据RFC 2045协议,一个Http Post的数据格式如下:
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="file"; filename="file1.dat"
Content-Type: application/octet-stream
... contents of file1.dat ...
--AaB03x--
详细解释如下:
Content-Type: multipart/form-data; boundary=AaB03x
如上面所示,首先声明数据类型为multipart/form-data, 然后定义边界字符串AaB03x,这个边界字符串就是用来在下面来区分各个数据的,可以随便定义,但是最好是用破折号等数据中一般不会出现的字符。然后是换行符。
注意:Post中定义的换行符是\r\n
--AaB03x
这个是边界字符串,注意每一个边界符前面都需要加2个连字符“--”,然后跟上换行符。
Content-Disposition: form-data; name="submit-name"
这里是Key-Value数据中字符串类型的数据。submit-name 是这个Key-Value数据中的Key。当然也需要换行符。
Larry
这个就是刚才Key-Value数据中的value。
--AaB03x
边界符,表示数据结束。
Content-Disposition: form-data; name="file"; filename="file1.dat"
这个代表另外一个数据,它的key是file,文件名是file1.dat。 注意:最后面没有分号了。
Content-Type: application/octet-stream
这个标识文件类型。application/octet-stream表示二进制数据。
... contents of file1.dat ...
这个是文件内容。可以使二进制的数据。
--AaB03x--
数据结束后的分界符,注意因为这个后面没有数据了所以需要在后面追加一个“--”表示结束。
C#下Post数据的函数
搞明白格式后,我们就很容易写出C#的代码了。如下所示:
private static string HttpPostData(string url, int timeOut, string fileKeyName,
string filePath, NameValueCollection stringDict)
{
string responseContent;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 设置属性
webRequest.Method = "POST";
webRequest.Timeout = timeOut;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
// 写入文件
const string filePartHeader =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n"+
"Content-Type: application/octet-stream\r\n\r\n";
var header = string.Format(filePartHeader, fileKeyName, filePath);
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(beginBoundary, 0, beginBoundary.Length);
memStream.Write(headerbytes, 0, headerbytes.Length);
var buffer = new byte[1024];
int bytesRead; // =0
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
// 写入字符串的Key
var stringKeyHeader = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\""+
"\r\n\r\n{1}\r\n";
foreach (byte[] formitembytes in from string key in stringDict.Keys
select string.Format(stringKeyHeader, key, stringDict[key])
into formitem
select Encoding.UTF8.GetBytes(formitem))
{
memStream.Write(formitembytes, 0, formitembytes.Length);
}
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
}
fileStream.Close();
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
参考资料
Rfc2045:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.
http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
========