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

来源:互联网 发布:全29青峰剃刀数据 编辑:程序博客网 时间:2024/05/21 17:19

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();  

  1. }  
============================================
private void button1_Click(object sender, EventArgs e)
        {
            HttpWebRequest _httpWebRequest= (HttpWebRequest)WebRequest.Create(@"http://cloud.bmob.cn/0906a62b462a3082/getMemberBySex");
            _httpWebRequest.Method = "POST";
            _httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            string _strContent="sex=boy";
            byte[] _byArr = Encoding.UTF8.GetBytes(_strContent);
            //_httpWebRequest.ContentLength = _byArr.Length;
            StreamWriter _streamWriter = new StreamWriter(_httpWebRequest.GetRequestStream(),Encoding.UTF8);
            _streamWriter.Write(_byArr);
            _streamWriter.Flush();
           HttpWebResponse _httpWebResponse= (HttpWebResponse)_httpWebRequest.GetResponse();
           string _strEncoding= _httpWebResponse.ContentEncoding;
           if (_strEncoding == null || _strEncoding.Length < 1)
           {
               _strEncoding = "UTF-8";
           }
           StreamReader _streamReader = new StreamReader(_httpWebResponse.GetResponseStream(),Encoding.GetEncoding(_strEncoding));
           string _strText= _streamReader.ReadToEnd();
           textBox1.Text = _strText;   
        }

0 0