C#使用GET、POST请求获取结果
来源:互联网 发布:nz挂机软件 编辑:程序博客网 时间:2024/05/21 14:00
C#使用GET、POST请求获取结果,这里以一个简单的用户登陆为例。
1、 使用GET请求获取结果
1.1 创建LoginHandler.aspx处理页面
<span style="font-size:18px;">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);}</span>
1.2 编写GET请求与获取结果方法
<span style="font-size:18px;">/// <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;}</span>
1.3 调用测试
<span style="font-size:18px;">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();}</span>
2、 使用POST请求获取结果
2.1 创建LoginHandler.aspx处理页面
<span style="font-size:18px;">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);}</span>
2.2 编写POST请求与获取结果方法
<span style="font-size:18px;">/// <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;}</span>
2.3 调用测试
<span style="font-size:18px;">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();}</span>
<span style="font-family: 'Microsoft YaHei'; line-height: 30px; text-align: right;font-size:18px; color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);"></span>
<span style="font-family: 'Microsoft YaHei'; line-height: 30px; text-align: right;font-size:18px; color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);"></span>
<span style="font-family: 'Microsoft YaHei'; line-height: 30px; text-align: right;font-size:18px; color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);">转自</span><a target=_blank href="http://blog.csdn.net/pan_junbiao/article/details/9155497" target="_blank" style="font-family: 'Microsoft YaHei'; line-height: 30px; text-align: right;font-size:18px; background-color: rgb(240, 240, 240); text-decoration: none;">pan_junbiao的专栏</a>
0 0
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C# HttpWebRequest使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果 (二)
- C# 通过GET、POST请求获取结果
- 使用GET,POST请求获取结果
- c#使用post,get请求网页
- C#请求网页(Get、Post)
- C#请求网页(Get、Post)
- 使用Unity 3D Get请求获取结果
- php获取get/post请求的数据
- php获取get/post请求的数据
- C#事件回调
- java 关于换行符,回车符的解释
- cocos2dx中使用触摸事件
- OpenStack企业私有云实践(笔记)
- 铭瑄 R9 270 显卡(1GB 版本)使用记
- C#使用GET、POST请求获取结果
- 面向对象
- C 中 & | ~ ^ 与 或 反 异或 的用法
- pip安装相关
- Entity Framework 对新数据库使用 Code First
- ExtJs4学习(十二)layout布局
- c语言应用
- can bind a LONG value only for insert into a LONG column
- 网上兼职是真的吗?淘宝兼职是真的吗?