当前位置:   article > 正文

C# HttpWebRequest使用GET、POST请求获取结果_httpwebrequest get

httpwebrequest get

原文链接:http://www.jb51.net/article/101901.htm
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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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(); 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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); 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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; 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号