自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析C# HTTP Request請求程序模擬

開發(fā) 后端
C# HTTP Request請求程序向你演示了在向服務(wù)器發(fā)送請求的模擬過程,那么具體的使用到的方法是什么呢?操作步驟是什么呢?那么本文就向你介紹詳細(xì)的內(nèi)容。

C# HTTP Request請求程序模擬是如何實(shí)現(xiàn)的呢?我們在實(shí)現(xiàn)發(fā)送請求的操作是會(huì)用到哪些方法呢?那么下面我們來看看具體的實(shí)現(xiàn)方法,使用下面的代碼片段時(shí),記得 在程序的引用上右鍵,然后添加引用,添加 System.Web. 就可以使用下面的代碼了.

C# HTTP Request請求程序模擬實(shí)例

  1. using System.Net;  
  2. using System.IO;  
  3. using System.Web;  
  4.  
  5. /********************  
  6. *C# HTTP Request請求程序模擬***  
  7.  * 向服務(wù)器送出請求  
  8.  * */ 
  9. public string SendRequest(string param)  
  10. {  
  11. ASCIIEncoding encoding = new ASCIIEncoding();  
  12. byte[] data = encoding.GetBytes(param);  
  13. HttpWebRequest request =   
  14. (HttpWebRequest)HttpWebRequest.Create(this.url);  
  15. request.Method = "POST";  
  16. request.ContentType = "application/x-www-form-urlencoded";  
  17. request.ContentLength = data.Length;  
  18. Stream sm = request.GetRequestStream();  
  19. sm.Write(data, 0, data.Length);  
  20. sm.Close();  
  21.  
  22. HttpWebResponse response =   
  23. (HttpWebResponse)request.GetResponse();  
  24.  
  25. if (response.StatusCode.ToString() != "OK")  
  26. {  
  27. MessageBox.Show(response.StatusDescription.ToString());  
  28. return "";  
  29. }  
  30.  
  31. StreamReader myreader = new StreamReader(  
  32. response.GetResponseStream(), Encoding.UTF8);  
  33. string responseText = myreader.ReadToEnd();  
  34. return responseText;  
  35. }  
  36. /**C# HTTP Request請求程序模擬  
  37.  * 進(jìn)行UTF-8的URL編碼轉(zhuǎn)換(針對漢字參數(shù))  
  38.  * */ 
  39. public string EncodeConver(string instring)  
  40. {  
  41. return HttpUtility.UrlEncode(instring, Encoding.UTF8);  
  42. }  
  43. /**C# HTTP Request請求程序模擬  
  44.  * 進(jìn)行登錄操作并返回相應(yīng)結(jié)果  
  45.  * */ 
  46. public bool DoLogin(string username,   
  47. string password)  
  48. {  
  49. password = System.Web.Security.FormsAuthentication.  
  50. HashPasswordForStoringInConfigFile(password, "MD5");  
  51. string param = string.Format("do=login&u={0}&p={1}",  
  52.  this.EncodeConver(username), this.EncodeConver(password));  
  53. string result = this.SendRequest(param);  
  54. // MessageBox.Show(result); 解析 Result ,我這里是作為一個(gè)XML Document來解析的  
  55. return true;  
  56. }  

C# HTTP Request請求程序模擬的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C# HTTP Request請求程序模擬有所幫助。

【編輯推薦】

  1. 詳解C#中不同類的類型
  2. 淺談C#中標(biāo)準(zhǔn)Dispose模式的實(shí)現(xiàn)
  3. C#選擇正確的集合進(jìn)行編碼
  4. C# 4.0新特性:協(xié)變與逆變中的編程思想
  5. C#應(yīng)用Attribute特性 代碼統(tǒng)計(jì)分析
責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2024-04-15 16:11:33

C#HTTP請求.NET

2009-09-07 10:34:47

2009-08-28 16:03:15

C#程序?qū)崿F(xiàn)鼠標(biāo)移動(dòng)

2009-08-14 11:00:16

C#創(chuàng)建Windows

2011-06-03 10:15:13

2009-09-07 14:00:57

C#抓取網(wǎng)頁

2009-08-07 17:25:37

C# SortedLi

2009-08-14 17:45:52

C# ArrayLis

2009-08-17 18:34:50

C# ChangeCo

2009-09-03 13:48:46

C#調(diào)用記事本記事本程序

2009-09-02 17:28:26

C#程序設(shè)計(jì)Windows窗體

2009-08-21 15:06:27

C#網(wǎng)絡(luò)聊天程序

2009-08-14 16:41:22

C#啟動(dòng)Windows

2009-08-25 17:59:49

C#入門

2009-08-25 09:39:21

創(chuàng)建C# Window

2009-08-20 16:15:19

C# 匿名方法

2009-07-31 14:03:21

C# Format函數(shù)

2009-08-18 09:24:52

C# Anonymou

2009-08-20 14:45:13

C# Switch語句

2009-08-10 17:36:17

C#擴(kuò)展方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號