C# 通过GET、POST请求获取结果
2017-03-02 18:09
387 查看
C#使用GET、POST请求获取结果,这里以一个简单的用户登陆为例。
1、 使用GET请求获取结果
1.1 创建LoginHandler.aspx处理页面
[csharp] view
plain copy
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请求与获取结果方法
[csharp] view
plain copy
/// <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 调用测试
[csharp] view
plain copy
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处理页面
[csharp] view
plain copy
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请求与获取结果方法
[csharp] view
plain copy
/// <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 调用测试
[csharp] view
plain copy
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、 使用GET请求获取结果
1.1 创建LoginHandler.aspx处理页面
[csharp] view
plain copy
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请求与获取结果方法
[csharp] view
plain copy
/// <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 调用测试
[csharp] view
plain copy
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处理页面
[csharp] view
plain copy
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请求与获取结果方法
[csharp] view
plain copy
/// <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 调用测试
[csharp] view
plain copy
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请求获取结果
- 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#使用GET、POST请求获取结果 (二)
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- 通过get或post请求URL获取返回数据
- C# HttpWebRequest使用GET、POST请求获取结果
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- android开发学习笔记(一)分别通过GET和POST请求从服务器端获取数据
- 使用GET,POST请求获取结果
- java通过Http get post请求url获取数据
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- BIM360: C#如何发送HTTP GET和POST请求登陆BIM 360 Glue以及获取项目列表