几个http请求相关的函数

1、 通过HttpWebRequest发起一个Post请求,并获取返回数据

 1 ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <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 ExpandedBlockStart.gifContractedBlock.gif         {
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)
17ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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))
30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
31                result = reader.ReadToEnd();
32            }

33            return result;
34        }

35

 

2、通过HttpWebRequest发起一个Get请求,并获取返回数据

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 2        /// 发送一个GET请求
 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 SendGetRequest(string baseurl, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
10ExpandedBlockStart.gifContractedBlock.gif        {
11            StringBuilder parassb = new StringBuilder();
12            foreach (string key in parameters.Keys)
13ExpandedSubBlockStart.gifContractedSubBlock.gif            {
14                if (parassb.Length > 0)
15                    parassb.Append("&");
16                parassb.AppendFormat("{0}={1}", HttpUtility.UrlEncode(key, reqencode), HttpUtility.UrlEncode(parameters[key], reqencode));
17            }

18
19            if (parassb.Length > 0)
20ExpandedSubBlockStart.gifContractedSubBlock.gif            {
21                baseurl += "?" + parassb;
22            }

23            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
24            req.Method = "GET";
25            req.MaximumAutomaticRedirections = 3;
26            req.Timeout = 5000;
27
28            string result = String.Empty;
29            using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
31                result = reader.ReadToEnd();
32            }

33            return result;
34
35        }

36
37

 

3、通过编程的方式实现Post提交请求并重定向至新的URL地址

 

 

 1 ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <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 ExpandedBlockStart.gifContractedBlock.gif         {
 9            StringBuilder script = new StringBuilder();
10            script.AppendFormat("<form name=redirpostform action='{0}' method='post'>",url);
11            foreach(string key in parameters.Keys)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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        }

 

转载于:https://www.cnblogs.com/mincyw/archive/2008/08/29/1279345.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值