欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-14 18:06:38
c#使用get、post请求获取结果,这里以一个简单的用户登陆为例。 1、 使用get请求获取结果 1.1 创建loginhandler.aspx处理页面 p...

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

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