post请求 application/x‐www‐form‐urlencoded

 

/// <summary>
        /// post请求
        /// 请求方法的编码格式:application/x‐www‐form‐urlencoded
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public string PostApi(string url,string data) {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "post";
            request.KeepAlive = true;
            byte[] buffer = encoding.GetBytes(data);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = reader.ReadToEnd();
            return result;
        }

使用

string url="http://www.ssss.com";
string
datastr = "method=" + method + "&requestData=" + requestData + "&appKey=" + appKey ;string res = PostApi(url,datastr);

 

posted @ 2021-04-24 17:24    阅读(407)  评论(0编辑  收藏  举报