zoukankan      html  css  js  c++  java
  • 向post请求中写入数据,最终保存在了HttpWebRequest.Params中

     一、向post请求中写入数据,最终保存在了HttpWebRequest.Params中:

      1)如果存入的是IDictionary类型的字符串变量,如:“username=administrator”,则key=value;

      2)如果写入的是string类型的变量,如"username",则key=null,value=username;

         

         protected void btnLogin_Click(object sender, EventArgs e)
            {
                
                string Url = "http://localhost:18472/DataRequest.aspx";
                string contentType = "application/x-www-form-urlencoded";
                string username = "administrator";
                string password = "admin";
                IDictionary<string,string> param=new Dictionary<string,string>();
                param.Add("username",username);
                param.Add("password",password);
                int i = 0;
                StringBuilder sb=new StringBuilder();
                foreach (var key in param.Keys)
                {
                    if (i>0)
                    {
                        sb.AppendFormat("&{0}={1}", key, param[key]);
                    }
                    else
                    {
                        sb.AppendFormat("{0}={1}", key, param[key]);
                    }
                    i++;
                }
                //string content = SendPost(sb.ToString(),Url,contentType);
                string content = SendPost(username,Url,contentType);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="data">写入流中的数据,最后将保存在request.params中,是Idictionary类型的变量;如果是没有等号的string类型,key=null,value=string</param>
            /// <param name="url">请求的url</param>
            /// <param name="contentType">请求的内容类型</param>
            /// <returns></returns>
            public static string SendPost(string data, string url, string contentType)
            {
                string content = string.Empty;
                HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = contentType;
                using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    sw.Write(data);
                }
                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.Default))
                {
                    content = sr.ReadToEnd();
                }
                //httpWebRequest.EndGetResponse;
                return content;
            }

     二、从request中取出写入的data数据(比如:xml等)的另一种方法:从流中获取(仅一次)

           string content="";
        using (Stream stream = HttpContext.Current.Request.InputStream)
           {
               using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
               {
                    content=sr.ReadToEnd();
               }
           }                    

    三、实现数据在IHttpModule和IHttpHandler之间(process项目和web项目之间)数据的组织和共享的键/值集合,保存在context.Items中

    string XML=content;
    HttpContext context=HttpContext.Current;
    context.Items.Add("XML", XML); 

    四、从Items集合中取出数据

    string XML=context.Items["XML"].ToString();
  • 相关阅读:
    java 调用Python
    RestController 能不能通过配置关闭
    java jdb
    Curator zookeeper
    「ASCII 流程图」工具——Graph Easy
    Python String Formatting Best Practices
    idea java9以及以上 出现找不到class的情况
    时间序列分析 异常分析 stl
    pip install whl
    t-SNE 层次聚类
  • 原文地址:https://www.cnblogs.com/slu182/p/4251972.html
Copyright © 2011-2022 走看看