C#使用GET、POST请求获取结果
作者:傲语平生 时间:2023-04-20 13:35:43
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#,GET,POST
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现层叠卡片式banner
2023-03-04 03:57:08
![](https://img.aspxhome.com/file/2023/2/91702_0s.jpg)
Java简易学生成绩系统写法实例
2021-08-18 08:06:03
C#调用带结构体指针Dll的方法
2022-06-14 19:55:32
C++结合QT实现带有优先级的计算器功能
2022-07-05 08:12:49
![](https://img.aspxhome.com/file/2023/9/109779_0s.png)
Windows下RabbitMQ安装及配置详解
2022-04-29 08:11:18
![](https://img.aspxhome.com/file/2023/1/62411_0s.png)
Java算法之时间复杂度和空间复杂度的概念和计算
2023-06-11 17:47:56
![](https://img.aspxhome.com/file/2023/9/79519_0s.png)
C语言实现简单的三子棋小游戏
2022-04-26 04:17:26
![](https://img.aspxhome.com/file/2023/0/109770_0s.jpg)
Java深入理解代码块的使用细节
2023-06-13 04:45:59
![](https://img.aspxhome.com/file/2023/0/76610_0s.png)
Java如何实现树的同构?
2023-11-28 09:55:19
![](https://img.aspxhome.com/file/2023/4/59084_0s.png)
java swing标准对话框具体实现
2021-12-30 02:48:50
![](https://img.aspxhome.com/file/2023/6/108196_0s.jpg)
c# 闭包的相关知识以及需要注意的地方
2023-12-11 01:27:54
![](https://img.aspxhome.com/file/2023/0/86980_0s.png)
C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比
2021-07-25 17:00:04
Java实现从Html文本中提取纯文本的方法
2023-06-18 20:28:26
深入了解c#多线程编程
2022-08-14 15:13:58
maven setting.xml文件配置禅定之旅
2023-01-08 12:07:19
Springmvc ajax跨域请求处理方法实例详解
2023-08-25 22:56:46
Android开发入门环境快速搭建实战教程
2022-06-17 15:59:41
![](https://img.aspxhome.com/file/2023/3/139473_0s.jpg)
一个Servlet是如何处理多个请求的?
2023-01-18 11:19:32
![](https://img.aspxhome.com/file/2023/5/70495_0s.png)
JDBC自定义连接池过程详解
2023-11-17 13:27:55
一篇文章带你入门Springboot沙箱环境支付宝支付(附源码)
2021-06-26 23:21:16
![](https://img.aspxhome.com/file/2023/6/67646_0s.png)