几个http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect
几个http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect
1、 通过HttpWebRequest发起一个Post请求,并获取返回数据
使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据
/// <summary>
/// 使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据
/// </summary>
/// <param name="url">请求的url地址</param>
/// <param name="parameters">请求的参数集合</param>
/// <param name="reqencode">请求的编码格式</param>
/// <param name="resencode">接收的编码格式</param>
/// <returns></returns>
public static string SendPostRequest(string url, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder parassb = new StringBuilder();
foreach (string key in parameters.Keys)
{
if (parassb.Length > 0)
parassb.Append("&");
parassb.AppendFormat("{0}={1}", key, parameters[key]);
}
byte[] data = reqencode.GetBytes(parassb.ToString());
Stream reqstream = req.GetRequestStream();
reqstream.Write(data, 0, data.Length);
reqstream.Close();
string result = String.Empty;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
{
result = reader.ReadToEnd();
}
return result;
}
/// <summary>
/// 使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据
/// </summary>
/// <param name="url">请求的url地址</param>
/// <param name="parameters">请求的参数集合</param>
/// <param name="reqencode">请求的编码格式</param>
/// <param name="resencode">接收的编码格式</param>
/// <returns></returns>
public static string SendPostRequest(string url, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder parassb = new StringBuilder();
foreach (string key in parameters.Keys)
{
if (parassb.Length > 0)
parassb.Append("&");
parassb.AppendFormat("{0}={1}", key, parameters[key]);
}
byte[] data = reqencode.GetBytes(parassb.ToString());
Stream reqstream = req.GetRequestStream();
reqstream.Write(data, 0, data.Length);
reqstream.Close();
string result = String.Empty;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
{
result = reader.ReadToEnd();
}
return result;
}
2、通过HttpWebRequest发起一个Get请求,并获取返回数据
HttpWebRequest 发送一个GET请求
/*
* 需要引用如下namespace
* using System.Reflection;
* using System.Text;
* using System.Net;
* using System.IO;
*/
/// <summary>
/// HttpWebRequest 发送一个GET请求
/// </summary>
/// <param name="url">请求的url地址</param>
/// <param name="parameters">请求的参数集合</param>
/// <param name="reqencode">请求的编码格式</param>
/// <param name="resencode">接收的编码格式</param>
/// <returns></returns>
public static string SendGetRequest(string baseurl, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
{
StringBuilder parassb = new StringBuilder();
foreach (string key in parameters.Keys)
{
if (parassb.Length > 0)
parassb.Append("&");
parassb.AppendFormat("{0}={1}", HttpUtility.UrlEncode(key, reqencode), HttpUtility.UrlEncode(parameters[key], reqencode));
}
if (parassb.Length > 0)
{
baseurl += "?" + parassb;
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
req.Method = "GET";
req.MaximumAutomaticRedirections = 3;
req.Timeout = 5000;
string result = String.Empty;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
{
result = reader.ReadToEnd();
}
return result;
}
/*
* 需要引用如下namespace
* using System.Reflection;
* using System.Text;
* using System.Net;
* using System.IO;
*/
/// <summary>
/// HttpWebRequest 发送一个GET请求
/// </summary>
/// <param name="url">请求的url地址</param>
/// <param name="parameters">请求的参数集合</param>
/// <param name="reqencode">请求的编码格式</param>
/// <param name="resencode">接收的编码格式</param>
/// <returns></returns>
public static string SendGetRequest(string baseurl, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
{
StringBuilder parassb = new StringBuilder();
foreach (string key in parameters.Keys)
{
if (parassb.Length > 0)
parassb.Append("&");
parassb.AppendFormat("{0}={1}", HttpUtility.UrlEncode(key, reqencode), HttpUtility.UrlEncode(parameters[key], reqencode));
}
if (parassb.Length > 0)
{
baseurl += "?" + parassb;
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
req.Method = "GET";
req.MaximumAutomaticRedirections = 3;
req.Timeout = 5000;
string result = String.Empty;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
{
result = reader.ReadToEnd();
}
return result;
}
3、通过编程的方式实现Post提交请求并重定向至新的URL地址
Post请求并且重定向
/// <summary>
/// Post请求并且重定向
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <param name="context"></param>
public static void PostAndRedirect(string url, NameValueCollection parameters, HttpContext context)
{
StringBuilder script = new StringBuilder();
script.AppendFormat("<form name=redirpostform action='{0}' method='post'>", url);
foreach (string key in parameters.Keys)
{
script.AppendFormat("<input type='hidden' name='{0}' value='{1}'>",
key, parameters[key]);
}
script.Append("</form>");
script.Append("<script language='javascript'>redirpostform.submit();</script>");
context.Response.Write(script);
context.Response.End();
}
/// <summary>
/// Post请求并且重定向
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <param name="context"></param>
public static void PostAndRedirect(string url, NameValueCollection parameters, HttpContext context)
{
StringBuilder script = new StringBuilder();
script.AppendFormat("<form name=redirpostform action='{0}' method='post'>", url);
foreach (string key in parameters.Keys)
{
script.AppendFormat("<input type='hidden' name='{0}' value='{1}'>",
key, parameters[key]);
}
script.Append("</form>");
script.Append("<script language='javascript'>redirpostform.submit();</script>");
context.Response.Write(script);
context.Response.End();
}
--