1、 通过HttpWebRequest发起一个Post请求,并获取返回数据
1 /// <summary>
2 /// 使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据
3 /// </summary>
4 /// <param name="url">请求的url地址</param>
5 /// <param name="parameters">请求的参数集合</param>
6 /// <param name="reqencode">请求的编码格式</param>
7 /// <param name="resencode">接收的编码格式</param>
8 /// <returns></returns>
9 public static string SendPostRequest(string url, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
10 {
11 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
12 req.Method = "post";
13 req.ContentType = "application/x-www-form-urlencoded";
14
15 StringBuilder parassb = new StringBuilder();
16 foreach (string key in parameters.Keys)
17 {
18 if (parassb.Length >0)
19 parassb.Append("&");
20 parassb.AppendFormat("{0}={1}", key,parameters[key]);
21 }
22 byte[] data = reqencode.GetBytes(parassb.ToString());
23 Stream reqstream = req.GetRequestStream();
24
25 reqstream.Write(data, 0, data.Length);
26 reqstream.Close();
27
28 string result = String.Empty;
29 using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
30 {
31 result = reader.ReadToEnd();
32 }
33 return result;
34 }
35
2 /// 使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据
3 /// </summary>
4 /// <param name="url">请求的url地址</param>
5 /// <param name="parameters">请求的参数集合</param>
6 /// <param name="reqencode">请求的编码格式</param>
7 /// <param name="resencode">接收的编码格式</param>
8 /// <returns></returns>
9 public static string SendPostRequest(string url, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
10 {
11 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
12 req.Method = "post";
13 req.ContentType = "application/x-www-form-urlencoded";
14
15 StringBuilder parassb = new StringBuilder();
16 foreach (string key in parameters.Keys)
17 {
18 if (parassb.Length >0)
19 parassb.Append("&");
20 parassb.AppendFormat("{0}={1}", key,parameters[key]);
21 }
22 byte[] data = reqencode.GetBytes(parassb.ToString());
23 Stream reqstream = req.GetRequestStream();
24
25 reqstream.Write(data, 0, data.Length);
26 reqstream.Close();
27
28 string result = String.Empty;
29 using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
30 {
31 result = reader.ReadToEnd();
32 }
33 return result;
34 }
35
2、通过HttpWebRequest发起一个Get请求,并获取返回数据
Code
3、通过编程的方式实现Post提交请求并重定向至新的URL地址
1 /// <summary>
2 /// Post请求并且重定向
3 /// </summary>
4 /// <param name="url"></param>
5 /// <param name="parameters"></param>
6 /// <param name="context"></param>
7 public static void PostAndRedirect(string url,NameValueCollection parameters,HttpContext context)
8 {
9 StringBuilder script = new StringBuilder();
10 script.AppendFormat("<form name=redirpostform action='{0}' method='post'>",url);
11 foreach(string key in parameters.Keys)
12 {
13 script.AppendFormat("<input type='hidden' name='{0}' value='{1}'>",
14 key,parameters[key]);
15 }
16 script.Append("</form>");
17 script.Append("<script language='javascript'>redirpostform.submit();</script>");
18 context.Response.Write(script);
19 context.Response.End();
20 }
2 /// Post请求并且重定向
3 /// </summary>
4 /// <param name="url"></param>
5 /// <param name="parameters"></param>
6 /// <param name="context"></param>
7 public static void PostAndRedirect(string url,NameValueCollection parameters,HttpContext context)
8 {
9 StringBuilder script = new StringBuilder();
10 script.AppendFormat("<form name=redirpostform action='{0}' method='post'>",url);
11 foreach(string key in parameters.Keys)
12 {
13 script.AppendFormat("<input type='hidden' name='{0}' value='{1}'>",
14 key,parameters[key]);
15 }
16 script.Append("</form>");
17 script.Append("<script language='javascript'>redirpostform.submit();</script>");
18 context.Response.Write(script);
19 context.Response.End();
20 }