C#使用GET、POST請求獲取結(jié)果
C#使用GET、POST請求獲取結(jié)果,這里以一個(gè)簡單的用戶登陸為例。
1、 使用GET請求獲取結(jié)果
1.1 創(chuàng)建LoginHandler.aspx處理頁面
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請求與獲取結(jié)果方法
/// <summary> /// GET請求與獲取結(jié)果 /// </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 調(diào)用測試
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請求獲取結(jié)果
2.1 創(chuàng)建LoginHandler.aspx處理頁面
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請求與獲取結(jié)果方法
/// <summary> /// POST請求與獲取結(jié)果 /// </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"; //默認(rèn)編碼 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); string retString = reader.ReadToEnd(); return retString; }
2.3 調(diào)用測試
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(); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用隊(duì)列(Queue)解決簡單的并發(fā)問題
這篇文章主要介紹了使用隊(duì)列(Queue)解決簡單的并發(fā)問題,講解的很細(xì)致,喜歡的朋友們可以了解一下2015-07-07c#將list類型轉(zhuǎn)換成DataTable方法示例
將List類型轉(zhuǎn)換成DataTable的通用方法,大家參考使用吧2013-12-12C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法
這篇文章主要介紹了C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法,涉及C#雙向鏈表的定義與排序技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08深入淺出掌握Unity ShaderLab語法基礎(chǔ)
Unity中所有Shader文件都通過一種陳述性語言進(jìn)行描述,稱為“ShaderLab”, 這篇文章主要介紹了Unity圖形學(xué)之ShaderLab入門基礎(chǔ),需要的朋友可以參考下2023-05-05原生實(shí)現(xiàn)C#與Lua相互調(diào)用方法(Unity3D可用)
Lua是一種很好的擴(kuò)展性語言,Lua解釋器被設(shè)計(jì)成一個(gè)很容易嵌入到宿主程序的庫,下面這篇文章主要給大家介紹了關(guān)于原生實(shí)現(xiàn)C#與Lua相互調(diào)用方法,Unity3D可用的相關(guān)資料,需要的朋友可以參考下2022-04-04