C#使用GET、POST請求獲取結果,這裡以一個簡單的用戶登陸為例。 1、 使用GET請求獲取結果 1.1 創建LoginHandler.aspx處理頁面 [csharp] view plain copy protected void Page_Load(object sender, EventAr ...
C#使用GET、POST請求獲取結果,這裡以一個簡單的用戶登陸為例。
1、 使用GET請求獲取結果
1.1 創建LoginHandler.aspx處理頁面
[csharp] view plain copy
- protected void Page_Load(object sender, EventArgs e)
- {
- string result = "";
- string userName = Request.QueryString["UserName"];
- string password = Request.QueryString["Password"];
- if (userName == "admin" && password == "123")
- {
- result = "登陸成功";
- }
- else
- {
- result = "登陸失敗";
- }
- Response.Write(result);
- }
1.2 編寫GET請求與獲取結果方法
[csharp] view plain copy
- /// <summary>
- /// GET請求與獲取結果
- /// </summary>
- public static string HttpGet(string Url, string postDataStr)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
- request.Method = "GET";
- request.ContentType = "text/html;charset=UTF-8";
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream myResponseStream = response.GetResponseStream();
- StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
- string retString = myStreamReader.ReadToEnd();
- myStreamReader.Close();
- myResponseStream.Close();
- return retString;
- }
1.3 調用測試
[csharp] view plain copy
- static void Main(string[] args)
- {
- string url = "http://www.mystudy.cn/LoginHandler.aspx";
- string data = "UserName=admin&Password=123";
- string result = HttpGet(url, data);
- Console.WriteLine(result);
- Console.ReadLine();
- }
2、 使用POST請求獲取結果
2.1 創建LoginHandler.aspx處理頁面
[csharp] view plain copy
- protected void Page_Load(object sender, EventArgs e)
- {
- string result = "";
- string userName = Request.Form["UserName"];
- string password = Request.Form["Password"];
- if (userName == "admin" && password == "123")
- {
- result = "登陸成功";
- }
- else
- {
- result = "登陸失敗";
- }
- Response.Write(result);
- }
2.2 編寫POST請求與獲取結果方法
[csharp] view plain copy
- /// <summary>
- /// POST請求與獲取結果
- /// </summary>
- public static string HttpPost(string Url, string postDataStr)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = postDataStr.Length;
- StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII);
- writer.Write(postDataStr);
- writer.Flush();
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- string encoding = response.ContentEncoding;
- if (encoding == null || encoding.Length < 1) {
- encoding = "UTF-8"; //預設編碼
- }
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
- string retString = reader.ReadToEnd();
- return retString;
- }
2.3 調用測試
[csharp] view plain copy
- static void Main(string[] args)
- {
- string url = "http://www.mystudy.cn/LoginHandler.aspx";
- string data = "UserName=admin&Password=123";
- string result = HttpPost(url, data);
- Console.WriteLine(result);
- Console.ReadLine();
- }