zoukankan      html  css  js  c++  java
  • C#使用GET、POST请求获取结果

    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. }  
  • 相关阅读:
    哈夫曼树
    MUI
    mui.init方法配置
    js中如何把字符串转化为对象、数组示例代码
    ( 转 )超级惊艳 10款HTML5动画特效推荐
    ( 转 ) 关于微信公众号,你不知道的15个小技巧
    h5预加载代码
    css3常用动画样式文件move.css
    iphone微信 h5页音乐自动播放
    sshpass: 用于非交互的ssh 密码验证
  • 原文地址:https://www.cnblogs.com/cyit/p/6243933.html
Copyright © 2011-2022 走看看