日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 操作http协议学习总结

發(fā)布時間:2025/4/14 C# 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 操作http协议学习总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C#中HttpWebRequest的用法詳解

http://www.jb51.net/article/57156.htm


這篇文章主要介紹了C#中HttpWebRequest的用法,以實例的形式詳細敘述了HttpWebRequest類中GET與POST的用法,非常具有參考借鑒價值,需要的朋友可以參考下


HttpWebRequest類主要利用HTTP 協(xié)議和服務器交互,通常是通過 GET 和 POST 兩種方式來對數(shù)據(jù)進行獲取和提交。下面對這兩種方式進行一下說明:


GET 方式:
GET 方式通過在網(wǎng)絡地址附加參數(shù)來完成數(shù)據(jù)的提交,比如在地址 http://www.jb51.net/?hl=zh-CN 中,前面部分 http://www.jb51.net表示數(shù)據(jù)提交的網(wǎng)址,后面部分 hl=zh-CN 表示附加的參數(shù),其中 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())
{
//在這里對接收到的頁面內(nèi)容進行處理
}
使用 GET 方式提交中文數(shù)據(jù)。


GET 方式通過在網(wǎng)絡地址中附加參數(shù)來完成數(shù)據(jù)提交,對于中文的編碼,常用的有 gb2312 和 utf8 兩種。
用 gb2312 方式編碼訪問的程序代碼如下:




復制代碼 代碼如下:Encoding myEncoding = Encoding.GetEncoding("gb2312");
string address = "http://www.jb51.net/?" + HttpUtility.UrlEncode("參數(shù)一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在這里對接收到的頁面內(nèi)容進行處理
}
在上面的程序代碼中,我們以 GET 方式訪問了網(wǎng)址 http://www.jb51.net ,傳遞了參數(shù)“參數(shù)一=值一”,由于無法告知對方提交數(shù)據(jù)的編碼類型,所以編碼方式要以對方的網(wǎng)站為標準。
?
POST 方式:


POST 方式通過在頁面內(nèi)容中填寫參數(shù)的方法來完成數(shù)據(jù)的提交,參數(shù)的格式和 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())
{
? ?//在這里對接收到的頁面內(nèi)容進行處理
}
在上面的代碼中,我們訪問了 http://www.jb51.net 的網(wǎng)址,分別以 GET 和 POST 方式提交了數(shù)據(jù),并接收了返回的頁面內(nèi)容。然而,如果提交的參數(shù)中含有中文,那么這樣的處理是不夠的,需要對其進行編碼,讓對方網(wǎng)站能夠識別。 ?
使用 POST 方式提交中文數(shù)據(jù)
POST 方式通過在頁面內(nèi)容中填寫參數(shù)的方法來完成數(shù)據(jù)的提交,由于提交的參數(shù)中可以說明使用的編碼方式,所以理論上能獲得更大的兼容性。
用 gb2312 方式編碼訪問的程序代碼如下: ?




復制代碼 代碼如下:Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode("參數(shù)一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding) + "&" + HttpUtility.UrlEncode("參數(shù)二", 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())
{
? ?//在這里對接收到的頁面內(nèi)容進行處理
}
從上面的代碼可以看出, POST 中文數(shù)據(jù)的時候,先使用 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>?
? ? ? ? /// ?獲取網(wǎng)址HTML?
? ? ? ? /// </summary>?
? ? ? ? /// <param name="URL">網(wǎng)址 </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>?
? ? ? ? /// 獲取網(wǎng)站cookie?
? ? ? ? /// </summary>?
? ? ? ? /// <param name="URL">網(wǎng)址 </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">網(wǎng)址 </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">網(wǎng)址 </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網(wǎng)址 </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監(jiān)聽處理Http請求

http://blog.sina.com.cn/s/blog_6f3ff2c90101wwh5.html


using 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("最大線程數(shù):{0}", maxThreadNum);
? ? ? ? ? ? ? ? ? ? Console.WriteLine("最小空閑線程數(shù):{0}", minThreadNum);


? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? //ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), x);
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? Console.WriteLine("\n\n等待客戶連接中。。。。");
? ? ? ? ? ? ? ? ? ? while (true)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //等待請求連接
? ? ? ? ? ? ? ? ? ? ? ? //沒有請求則GetContext處于阻塞狀態(tài)
? ? ? ? ? ? ? ? ? ? ? ? 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狀態(tài)代碼
? ? ? ?
? ? ? ? ? ? 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上右鍵,以管理員身份運行,然后執(zhí)行下面的命令
? ?netsh http add urlacl url=http://本機IP:1500/ user=用戶名(如Administrator)
? ?
3.記得關閉防火墻,或者只開放指定端口,步驟如下:
? ? ? ? step1、點擊控制面板
  
  step2、選擇windows防火墻,點擊高級設置
  
  step3、在彈出的“高級安全windows防火墻”點擊“入站規(guī)則”,在右側“操作”欄點擊“入站規(guī)則”下的“新建規(guī)則…”,此時會彈出一個窗口讓你設置。剩下的就非常傻瓜化了。
  
  step4、彈出“新建入站規(guī)則向導”-規(guī)則類型-選中“端口”,點擊下一步。選擇規(guī)則應用的協(xié)議“TCP/UDP”如果是TCP你就選擇TCP,UDP就選擇UDP。再勾選“特定本地端口”在文本框輸入您想開放的端口號(例如1521)。
  
  step5、點擊下一步,到“連接符合指定條件時應該進行什么操作?”選擇“允許連接”。點擊下一步到“配置文件”何時應用該規(guī)則,勾選“域”、“專用”、“公用”點擊下一步。
  
  step6、配置規(guī)則名稱,隨便輸入你自己認為好記的規(guī)則名稱即可。
========

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 應用程序的編程接口, 微軟稱之為“現(xiàn)代化的 HTTP 編程接口”, 主要提供如下內(nèi)容:


1. 用戶通過 HTTP 使用現(xiàn)代化的 Web Service 的客戶端組件;


2. 能夠同時在客戶端與服務端同時使用的 HTTP 組件(比如處理 HTTP 標頭和消息), 為客戶端和服務端提供一致的編程模型。


個人看來是抄襲 apache http client ,目前網(wǎng)上用的人好像不多, 個人認為使用httpclient最大的好處是:不用自己管理cookie,只要負責寫好請求即可。


由于網(wǎng)上資料不多,這里借登錄博客園網(wǎng)站做個簡單的總結其get和post請求的用法。


查看微軟的api可以發(fā)現(xiàn)其屬性方法: http://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.aspx


由其api可以看出如果想 設置請求頭 只需要在 DefaultRequestHeaders 里進行設置


創(chuàng)建httpcliet可以直接new HttpClient()


發(fā)送請求 可以按發(fā)送方式分別調用其方法,如 get調用 GetAsync(Uri) , post調用 PostAsync(Uri, HttpContent) ,其它依此類推。。。


二、實例(模擬post登錄博客園)


首先,需要說明的是, 本實例環(huán)境是win7 64位+vs 2013+ .net 4.5框架。


1.使用vs2013新建一個控制臺程序,或者窗體程序,如下圖所示:




2.必須引入System.Net.Http框架,否則將不能使用httpclient


3.實現(xiàn)代碼


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函數(shù)開始,調用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登錄后的截圖:


總結,可以發(fā)現(xiàn)C#中HttpClient的用法和Java中非常相似,所以,說其抄襲確實不為過。
========

在C#用HttpWebRequest中發(fā)送GET/HTTP/HTTPS請求

http://zhoufoxcn.blog.51cto.com/792419/561934/


?這個需求來自于我最近練手的一個項目,在項目中我需要將一些自己發(fā)表的和收藏整理的網(wǎng)文集中到一個地方存放,如果全部采用手工操作工作量大而且繁瑣,因此周公決定利用C#來實現(xiàn)。在很多地方都需要驗證用戶身份才可以進行下一步操作,這就免不了POST請求來登錄,在實際過程中發(fā)現(xiàn)有些網(wǎng)站登錄是HTTPS形式的,在解決過程中遇到了一些小問題,現(xiàn)在跟大家分享。
?通用輔助類
?下面是我編寫的一個輔助類,在這個類中采用了HttpWebRequest中發(fā)送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 ? ?
?* 版權說明:本文可以在保留原文出處的情況下使用于非商業(yè)用途,周公對此不作任何擔保或承諾。 ? ?
?* */?
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> ?
? ? ? ? /// 創(chuàng)建GET方式的HTTP請求 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="url">請求的URL</param> ?
? ? ? ? /// <param name="timeout">請求的超時時間</param> ?
? ? ? ? /// <param name="userAgent">請求的客戶端瀏覽器信息,可以為空</param> ?
? ? ? ? /// <param name="cookies">隨同HTTP請求發(fā)送的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> ?
? ? ? ? /// 創(chuàng)建POST方式的HTTP請求 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="url">請求的URL</param> ?
? ? ? ? /// <param name="parameters">隨同請求POST的參數(shù)名稱及參數(shù)值字典</param> ?
? ? ? ? /// <param name="timeout">請求的超時時間</param> ?
? ? ? ? /// <param name="userAgent">請求的客戶端瀏覽器信息,可以為空</param> ?
? ? ? ? /// <param name="requestEncoding">發(fā)送HTTP請求時所用的編碼</param> ?
? ? ? ? /// <param name="cookies">隨同HTTP請求發(fā)送的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; ?
? ? ? ? ? ? //如果是發(fā)送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數(shù)據(jù) ?
? ? ? ? ? ? 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數(shù)據(jù)到HTTP和HTTPS站點不同,POST數(shù)據(jù)到HTTPS站點的時候需要設置ServicePointManager類的ServerCertificateValidationCallback屬性,并且在POST到https://passport.baidu.com/?login時還需要將HttpWebResquest實例的ProtocolVersion屬性設置為HttpVersion.Version10(這個未驗證是否所有的HTTPS站點都需要設置),否則在調用GetResponse()方法時會拋出“基礎連接已經(jīng)關閉: 連接被意外關閉。”的異常。
?
?用法舉例
?這個類用起來也很簡單:
?(1)POST數(shù)據(jù)到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)發(fā)送GET請求到HTTP站點
?在cookieString中包含了服務器端返回的會話信息數(shù)據(jù),從中提取了之后可以設置Cookie下次登錄時帶上這個Cookie就可以以認證用戶的信息,假設我們已經(jīng)登錄成功并且獲取了Cookie,那么發(fā)送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)發(fā)送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#中發(fā)送請求到HTTP和HTTPS的用法,分GET/POST兩種方式,為減少一些繁瑣和機械的編碼,周公將其封裝為一個類,發(fā)送數(shù)據(jù)之后返回HttpWebResponse對象實例,利用這個實例我們可以獲取服務器端返回的Cookie以便用認證用戶的身份繼續(xù)發(fā)送請求,或者讀取服務器端響應的內(nèi)容,不過在讀取響應內(nèi)容時要注意響應格式和編碼,本來在這個類中還有讀取HTML和WML內(nèi)容的方法(包括服務器使用壓縮方式傳輸?shù)臄?shù)據(jù)),但限于篇幅和其它方面的原因,此處省略掉了。如有機會,在以后的文章中會繼續(xù)講述這方面的內(nèi)容。
========

http協(xié)議post數(shù)據(jù)標準格式 ?

http://blog.163.com/wu_ernan/blog/static/2102880202014226102528240/


在C#中有HttpWebRequest類,可以很方便用來獲取http請求,但是這個類對Post方式?jīng)]有提供一個很方便的方法來獲取數(shù)據(jù)。網(wǎng)上有很多人提供了解決方法,但都參差不齊,這里我把我使用的方法總結出來,與大家分享。
?
本文精華:實現(xiàn)了post的時候即可以有字符串的key-value,還可以帶文件。
?
Post數(shù)據(jù)格式
?
Post提交數(shù)據(jù)的時候最重要就是把Key-Value的數(shù)據(jù)放到http請求流中,而HttpWebRequest沒有提供一個屬性之類的東西可以讓我們自由添加Key-Value,因此就必須手工構造這個數(shù)據(jù)。
根據(jù)RFC 2045協(xié)議,一個Http Post的數(shù)據(jù)格式如下:
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
?
如上面所示,首先聲明數(shù)據(jù)類型為multipart/form-data, 然后定義邊界字符串AaB03x,這個邊界字符串就是用來在下面來區(qū)分各個數(shù)據(jù)的,可以隨便定義,但是最好是用破折號等數(shù)據(jù)中一般不會出現(xiàn)的字符。然后是換行符。
注意:Post中定義的換行符是\r\n
--AaB03x
?
這個是邊界字符串,注意每一個邊界符前面都需要加2個連字符“--”,然后跟上換行符。
Content-Disposition: form-data; name="submit-name"
?
這里是Key-Value數(shù)據(jù)中字符串類型的數(shù)據(jù)。submit-name 是這個Key-Value數(shù)據(jù)中的Key。當然也需要換行符。
Larry
?
這個就是剛才Key-Value數(shù)據(jù)中的value。
--AaB03x
?
邊界符,表示數(shù)據(jù)結束。
Content-Disposition: form-data; name="file"; filename="file1.dat"
?
這個代表另外一個數(shù)據(jù),它的key是file,文件名是file1.dat。 注意:最后面沒有分號了。
Content-Type: application/octet-stream
?
這個標識文件類型。application/octet-stream表示二進制數(shù)據(jù)。
... contents of file1.dat ...
?
這個是文件內(nèi)容。可以使二進制的數(shù)據(jù)。
--AaB03x--
?
數(shù)據(jù)結束后的分界符,注意因為這個后面沒有數(shù)據(jù)了所以需要在后面追加一個“--”表示結束。
C#下Post數(shù)據(jù)的函數(shù)
?
搞明白格式后,我們就很容易寫出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
========

總結

以上是生活随笔為你收集整理的C# 操作http协议学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。