zoukankan      html  css  js  c++  java
  • C#模拟HTTP POST 请求

    GET请求:

     /// <summary>
            /// 获取accessToken
            /// </summary>
            /// <param name="corpid">ID</param>
            /// <param name="corpsecret">密钥</param>
            /// <returns></returns>
            static string GetQYAccessToken(string corpid, string corpsecret)
            {
                string getAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
                string accessToken = "";
    
                string respText = "";
    
                //获取josn数据
                string url = string.Format(getAccessTokenUrl, corpid, corpsecret);
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                using (Stream resStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(resStream, Encoding.Default);
                    respText = reader.ReadToEnd();
                    resStream.Close();
                }
    
                try
                {
                    JavaScriptSerializer Jss = new JavaScriptSerializer();
                    Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
    
                    //通过键access_token获取值
                    accessToken = respDic["access_token"].ToString();
                }
                catch (Exception ex) { }
    
                return accessToken;
            }

    带参数POST请求:

    /// <summary>
            /// Post数据接口
            /// </summary>
            /// <param name="postUrl">接口地址</param>
            /// <param name="paramData">提交json数据</param>
            /// <param name="dataEncode">编码方式</param>
            /// <returns></returns>
            static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
            {
                string ret = string.Empty;
                try
                {
                    byte[] byteArray = dataEncode.GetBytes(paramData); //转化
                    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                    webReq.Method = "POST";
                    webReq.ContentType = "application/x-www-form-urlencoded";
    
                    webReq.ContentLength = byteArray.Length;
                    Stream newStream = webReq.GetRequestStream();
                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                    newStream.Close();
                    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    ret = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                    newStream.Close();
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
                return ret;
            }
    
  • 相关阅读:
    兄弟连学python(1)——MySQL
    运算和运算符相关知识
    关于python中的快捷键
    关于爬虫
    Hello Python
    [ARC101C] Ribbons on Tree
    CF568E Longest Increasing Subsequence
    2021省选游记
    [NEERC2015]Distance on Triangulation
    dp的一些优化
  • 原文地址:https://www.cnblogs.com/25miao/p/9382370.html
Copyright © 2011-2022 走看看