zoukankan      html  css  js  c++  java
  • 在 WinForm 中打开页面采用POST方式传参http。可以多个参数传递,返回json字符串

    //调用方法

    Dictionary<stringstring> postData = new Dictionary<stringstring>();
    postData.Add("user""aaa");
    postData.Add("pass""bbb");
     
    GetPageByPost("http://www.xxx.com/send.aspx", postData, Encoding.UTF8);
     
    /// <summary>
    /// 以 Post 方式提交网页数据,获得服务器返回的数据
    /// </summary>
    /// <param name="url"> Url </param>
    /// <param name="postData">Post 数据</param>
    /// <param name="encoder">网页编码</param>
    /// <returns>服务器返回的数据</returns>
    public string GetPageByPost(string url, Dictionary<stringstring> postData, Encoding encoder)
    {
        string html = "";
     
        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
        webReq.Method = "POST";
     
        Stream reqStream = null;
        if (postData != null && postData.Count > 0) {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<stringstring> kv in postData) {
                sb.Append(HttpUtility.UrlEncode(kv.Key));
                sb.Append("=");
                sb.Append(HttpUtility.UrlEncode(kv.Value));
                sb.Append("&");
            }
     
            byte[] data = Encoding.UTF8.GetBytes(sb.ToString().TrimEnd('&'));
     
            webReq.ContentType = ContentType;
            webReq.ContentLength = data.Length;
            reqStream = webReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
        }
     
        HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
        Stream stream = webResp.GetResponseStream();
        StreamReader sr = new StreamReader(stream, encoder);
        html = sr.ReadToEnd();
     
        sr.Close();
        stream.Close();
     
        if (reqStream != null) {
            reqStream.Close();
        }
     
        return html;
    }
  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/luoqin520/p/4645431.html
Copyright © 2011-2022 走看看