程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#運用GET、POST懇求獲取後果

C#運用GET、POST懇求獲取後果

編輯:C#入門知識

C#運用GET、POST懇求獲取後果。本站提示廣大學習愛好者:(C#運用GET、POST懇求獲取後果)文章只能為提供參考,不一定能成為您想要的結果。以下是C#運用GET、POST懇求獲取後果正文


C#運用GET、POST懇求獲取後果,這裡以一個復雜的用戶登陸為例。

1、 運用GET懇求獲取後果

1.1 創立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懇求與獲取後果辦法

/// <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 調用測試

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處置頁面

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懇求與獲取後果辦法

/// <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 調用測試

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

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved