C#使用GET、POST请求获取结果

原文:http://blog.csdn.net/pan_junbiao/article/details/9155497

C#使用GET、POST请求获取结果,这里以一个简单的用户登陆为例。

1、 使用GET请求获取结果

1.1 创建LoginHandler.aspx处理页面

[csharp] view plain copy
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     string result = "";  
  4.     string userName = Request.QueryString["UserName"];  
  5.     string password = Request.QueryString["Password"];  
  6.   
  7.     if (userName == "admin" && password == "123")  
  8.     {  
  9.         result = "登陆成功";  
  10.     }  
  11.     else  
  12.     {  
  13.         result = "登陆失败";  
  14.     }  
  15.     Response.Write(result);  
  16. }  

1.2 编写GET请求与获取结果方法

[csharp] view plain copy
  1. /// <summary>  
  2. /// GET请求与获取结果  
  3. /// </summary>  
  4. public static string HttpGet(string Url, string postDataStr)  
  5. {  
  6.     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);  
  7.     request.Method = "GET";  
  8.     request.ContentType = "text/html;charset=UTF-8";  
  9.   
  10.     HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  11.     Stream myResponseStream = response.GetResponseStream();  
  12.     StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);  
  13.     string retString = myStreamReader.ReadToEnd();  
  14.     myStreamReader.Close();  
  15.     myResponseStream.Close();  
  16.   
  17.     return retString;  
  18. }  

1.3 调用测试

[csharp] view plain copy
  1. static void Main(string[] args)  
  2. {  
  3.     string url = "http://www.mystudy.cn/LoginHandler.aspx";  
  4.     string data = "UserName=admin&Password=123";  
  5.     string result = HttpGet(url, data);  
  6.     Console.WriteLine(result);  
  7.     Console.ReadLine();  
  8. }  

2、 使用POST请求获取结果

2.1 创建LoginHandler.aspx处理页面

[csharp] view plain copy
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     string result = "";  
  4.     string userName = Request.Form["UserName"];  
  5.     string password = Request.Form["Password"];  
  6.   
  7.     if (userName == "admin" && password == "123")  
  8.     {  
  9.         result = "登陆成功";  
  10.     }  
  11.     else  
  12.     {  
  13.         result = "登陆失败";  
  14.     }  
  15.     Response.Write(result);  
  16. }  

2.2 编写POST请求与获取结果方法

[csharp] view plain copy
  1. /// <summary>  
  2. /// POST请求与获取结果  
  3. /// </summary>  
  4. public static string HttpPost(string Url, string postDataStr)  
  5. {  
  6.     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  
  7.     request.Method = "POST";  
  8.     request.ContentType = "application/x-www-form-urlencoded";  
  9.     request.ContentLength = postDataStr.Length;  
  10.     StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII);  
  11.     writer.Write(postDataStr);  
  12.     writer.Flush();  
  13.     HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  14.     string encoding = response.ContentEncoding;  
  15.     if (encoding == null || encoding.Length < 1) {  
  16.         encoding = "UTF-8"//默认编码  
  17.     }  
  18.     StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));  
  19.     string retString = reader.ReadToEnd();  
  20.     return retString;  
  21. }  

2.3 调用测试

[csharp] view plain copy
  1. static void Main(string[] args)  
  2. {  
  3.     string url = "http://www.mystudy.cn/LoginHandler.aspx";  
  4.     string data = "UserName=admin&Password=123";  
  5.     string result = HttpPost(url, data);  
  6.     Console.WriteLine(result);  
  7.     Console.ReadLine();  
  8. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值