欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

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

栏    目:C#教程

下一篇:利用C#如何给PDF文档添加文本与图片页眉

本文标题:C#使用GET、POST请求获取结果

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6038.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有