|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1729418
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2010/6/2 2:17 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]淺析C# HTTP Request請求程序模擬
- C# HTTP Request請求程序向你演示了在向服務器發送請求的模擬過程,那麼具體的使用到的方法是什麼呢?操作步驟是什麼呢?那麼本文就向你介紹詳細的內容。
C# HTTP Request請求程序模擬是如何實現的呢?我們在實現發送請求的操作是會用到哪些方法呢?那麼下面我們來看看具體的實現方法,使用下面的代碼片段時,記得 在程序的引用上右鍵,然後添加引用,添加 System.Web. 就可以使用下面的代碼了.
C# HTTP Request請求程序模擬實例
using System.Net;
using System.IO;
using System.Web;
/********************
**C# HTTP Request請求程序模擬***
* * 向服務器送出請求 * */
public string SendRequest(string param) {
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(param);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream sm = request.GetRequestStream();
sm.Write(data, 0, data.Length);
sm.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode.ToString() != "OK") {
MessageBox.Show(response.StatusDescription.ToString());
return "";
}
StreamReader myreader = new StreamReader( response.GetResponseStream(), Encoding.UTF8);
string responseText = myreader.ReadToEnd();
return responseText;
}
/**C# HTTP Request請求程序模擬
** 進行UTF-8的URL編碼轉換(針對漢字參數)
* * */
public string EncodeConver(string instring) {
return HttpUtility.UrlEncode(instring, Encoding.UTF8);
}
/**C# HTTP Request請求程序模擬
** 進行登錄操作並返回相應結果
* * */
public bool DoLogin(string username, string password) {
password = System.Web.Security.FormsAuthentication. HashPasswordForStoringInConfigFile(password, "MD5");
string param = string.Format("do=login&u={0}&p={1}", this.EncodeConver(username), this.EncodeConver(password));
string result = this.SendRequest(param);
// MessageBox.Show(result); 解析 Result ,我這裡是作為一個XML Document來解析的
return true;
}
C# HTTP Request請求程序模擬的基本內容就向你介紹到這裡,希望對你瞭解和學習C# HTTP Request請求程序模擬有所幫助。
【責任編輯:李彥光 TEL:(010)68476606】
原文出處:浅析C# HTTP Request请求程序模拟 - 51CTO.COM
|
|
|
討論串
|