茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672385 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00081.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2010/6/2 2:17:09
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
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|