zoukankan      html  css  js  c++  java
  • C# http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect

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

     1 使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据 
     2 
     3 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
     4     /// <summary>
     5     /// 使用指定编码格式发送一个POST请求,并通过约定的编码格式获取返回的数据
     6     /// </summary>
     7     /// <param name="url">请求的url地址</param>
     8     /// <param name="parameters">请求的参数集合</param>
     9     /// <param name="reqencode">请求的编码格式</param>
    10     /// <param name="resencode">接收的编码格式</param>
    11     /// <returns></returns>
    12     public static string SendPostRequest(string url, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
    13     {
    14         HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    15         req.Method = "post";
    16         req.ContentType = "application/x-www-form-urlencoded";
    17 
    18         StringBuilder parassb = new StringBuilder();
    19         foreach (string key in parameters.Keys)
    20         {
    21             if (parassb.Length > 0)
    22                 parassb.Append("&");
    23             parassb.AppendFormat("{0}={1}", key, parameters[key]);
    24         }
    25         byte[] data = reqencode.GetBytes(parassb.ToString());
    26         Stream reqstream = req.GetRequestStream();
    27         reqstream.Write(data, 0, data.Length);
    28         reqstream.Close();
    29         string result = String.Empty;
    30         using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
    31         {
    32             result = reader.ReadToEnd();
    33         }
    34         return result;
    35     }
    View Code

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

     1 HttpWebRequest 发送一个GET请求 
     2 
     3 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
     4 
     5     /*
     6      * 需要引用如下namespace
     7      * using System.Reflection;
     8      * using System.Text;
     9      * using System.Net;
    10      * using System.IO;
    11      */
    12 
    13     /// <summary>
    14     /// HttpWebRequest 发送一个GET请求
    15     /// </summary>
    16     /// <param name="url">请求的url地址</param>
    17     /// <param name="parameters">请求的参数集合</param>
    18     /// <param name="reqencode">请求的编码格式</param>
    19     /// <param name="resencode">接收的编码格式</param>
    20     /// <returns></returns>
    21     public static string SendGetRequest(string baseurl, NameValueCollection parameters, Encoding reqencode, Encoding resencode)
    22     {
    23         StringBuilder parassb = new StringBuilder();
    24         foreach (string key in parameters.Keys)
    25         {
    26             if (parassb.Length > 0)
    27                 parassb.Append("&");
    28             parassb.AppendFormat("{0}={1}", HttpUtility.UrlEncode(key, reqencode), HttpUtility.UrlEncode(parameters[key], reqencode));
    29         }
    30 
    31         if (parassb.Length > 0)
    32         {
    33             baseurl += "?" + parassb;
    34         }
    35         HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
    36         req.Method = "GET";
    37         req.MaximumAutomaticRedirections = 3;
    38         req.Timeout = 5000;
    39 
    40         string result = String.Empty;
    41         using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), resencode))
    42         {
    43             result = reader.ReadToEnd();
    44         }
    45         return result;
    46     }
    View Code

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

     1 Post请求并且重定向 
     2 
     3 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
     4     /// <summary>
     5     /// Post请求并且重定向
     6     /// </summary>
     7     /// <param name="url"></param>
     8     /// <param name="parameters"></param>
     9     /// <param name="context"></param>
    10     public static void PostAndRedirect(string url, NameValueCollection parameters, HttpContext context)
    11     {
    12         StringBuilder script = new StringBuilder();
    13         script.AppendFormat("<form name=redirpostform action='{0}' method='post'>", url);
    14         foreach (string key in parameters.Keys)
    15         {
    16             script.AppendFormat("<input type='hidden' name='{0}' value='{1}'>",
    17                 key, parameters[key]);
    18         }
    19         script.Append("</form>");
    20         script.Append("<script language='javascript'>redirpostform.submit();</script>");
    21         context.Response.Write(script);
    22         context.Response.End();
    23     }
    View Code

    感谢:http://www.cnblogs.com/didi/archive/2010/06/03/1750723.html

  • 相关阅读:
    如何改变常用编辑器(eclipse)的字号大小
    IOConsole Updater error
    RNAfold的使用方法
    单因素方差分析(oneway ANOVA)
    Perl内部保留变量(系统变量)
    Perl 中的正则表达式
    Perl Eclipse 格式化代码
    卸载Oracle 9i
    Ubuntu的菜鸟常用的基础命令
    linux as4u2 下安装openssh
  • 原文地址:https://www.cnblogs.com/lewisli/p/4129012.html
Copyright © 2011-2022 走看看