欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#使用GET、POST請求獲取結(jié)果

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

C#使用GET、POST請求獲取結(jié)果,這里以一個簡單的用戶登陸為例。

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"; //默認編碼 
  } 
  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)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:C#中倒序輸出字符串的方法示例

欄    目:C#教程

下一篇:利用C#如何給PDF文檔添加文本與圖片頁眉

本文標題:C#使用GET、POST請求獲取結(jié)果

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6038.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有