浅析C# HTTP Request请求程序模拟

开发 后端
C# HTTP Request请求程序向你演示了在向服务器发送请求的模拟过程,那么具体的使用到的方法是什么呢?操作步骤是什么呢?那么本文就向你介绍详细的内容。

C# HTTP Request请求程序模拟是如何实现的呢?我们在实现发送请求的操作是会用到哪些方法呢?那么下面我们来看看具体的实现方法,使用下面的代码片段时,记得 在程序的引用上右键,然后添加引用,添加 System.Web. 就可以使用下面的代码了.

C# HTTP Request请求程序模拟实例

  1. using System.Net;  
  2. using System.IO;  
  3. using System.Web;  
  4.  
  5. /********************  
  6. *C# HTTP Request请求程序模拟***  
  7.  * 向服务器送出请求  
  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.  * 进行UTF-8的URL编码转换(针对汉字参数)  
  38.  * */ 
  39. public string EncodeConver(string instring)  
  40. {  
  41. return HttpUtility.UrlEncode(instring, Encoding.UTF8);  
  42. }  
  43. /**C# HTTP Request请求程序模拟  
  44.  * 进行登录操作并返回相应结果  
  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 ,我这里是作为一个XML Document来解析的  
  55. return true;  
  56. }  

C# HTTP Request请求程序模拟的基本内容就向你介绍到这里,希望对你了解和学习C# HTTP Request请求程序模拟有所帮助。

【编辑推荐】

  1. 详解C#中不同类的类型
  2. 浅谈C#中标准Dispose模式的实现
  3. C#选择正确的集合进行编码
  4. C# 4.0新特性:协变与逆变中的编程思想
  5. C#应用Attribute特性 代码统计分析
责任编辑:仲衡 来源: 百度空间
相关推荐

2024-04-15 16:11:33

C#HTTP请求.NET

2009-08-14 11:00:16

C#创建Windows

2009-08-28 16:03:15

C#程序实现鼠标移动

2009-09-07 10:34:47

2009-09-07 14:00:57

C#抓取网页

2011-06-03 10:15:13

2009-08-07 17:25:37

C# SortedLi

2009-08-17 18:34:50

C# ChangeCo

2009-08-14 17:45:52

C# ArrayLis

2009-08-25 17:59:49

C#入门

2009-08-25 09:39:21

创建C# Window

2009-08-21 15:06:27

C#网络聊天程序

2009-09-02 17:28:26

C#程序设计Windows窗体

2009-09-03 13:48:46

C#调用记事本记事本程序

2009-08-14 16:41:22

C#启动Windows

2009-08-18 10:30:30

C#枚举

2009-08-27 13:30:11

C# interfac

2009-08-20 16:15:19

C# 匿名方法

2009-08-20 14:45:13

C# Switch语句

2009-08-27 11:43:31

C#语法
点赞
收藏

51CTO技术栈公众号