C# HTTP Request請求程序向你演示了在向服務(wù)器發(fā)送請求的模擬過程,,那么具體的使用到的方法是什么呢,?操作步驟是什么呢?那么本文就向你介紹詳細(xì)的內(nèi)容。
C# HTTP Request請求程序模擬是如何實現(xiàn)的呢?我們在實現(xiàn)發(fā)送請求的操作是會用到哪些方法呢,?那么下面我們來看看具體的實現(xiàn)方法,使用下面的代碼片段時,記得 在程序的引用上右鍵,然后添加引用,添加 System.Web. 就可以使用下面的代碼了.
C# HTTP Request請求程序模擬實例
- using System.Net;
- using System.IO;
- using System.Web;
-
-
-
-
-
- public string SendRequest(string param)
- {
- ASCIIEncoding encoding = new ASCIIEncoding();
- byte[] data = encoding.GetBytes(param);
- HttpWebRequest request =
- (HttpWebRequest)HttpWebRequest.Create(this.url);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = data.Length;
- Stream sm = request.GetRequestStream();
- sm.Write(data, 0, data.Length);
- sm.Close();
-
- HttpWebResponse response =
- (HttpWebResponse)request.GetResponse();
-
- if (response.StatusCode.ToString() != "OK")
- {
- MessageBox.Show(response.StatusDescription.ToString());
- return "";
- }
-
- StreamReader myreader = new StreamReader(
- response.GetResponseStream(), Encoding.UTF8);
- string responseText = myreader.ReadToEnd();
- return responseText;
- }
-
-
-
- public string EncodeConver(string instring)
- {
- return HttpUtility.UrlEncode(instring, Encoding.UTF8);
- }
-
-
-
- public bool DoLogin(string username,
- string password)
- {
- password = System.Web.Security.FormsAuthentication.
- HashPasswordForStoringInConfigFile(password, "MD5");
- string param = string.Format("do=login&u={0}&p={1}",
- this.EncodeConver(username), this.EncodeConver(password));
- string result = this.SendRequest(param);
-
- return true;
- }
C# HTTP Request請求程序模擬的基本內(nèi)容就向你介紹到這里,,希望對你了解和學(xué)習(xí)C# HTTP Request請求程序模擬有所幫助,。