首页 > 移动平台 > 详细

C# POST application/x-www-form-urlencoded 请求

时间:2021-03-03 19:22:08      阅读:25      评论:0      收藏:0      [点我收藏+]

 

 

public static string HttpPostFrom(string url, string data)
        {
            string htmlAll = "";
            try
            {                
                string SendMessageAddress = url;//请求链接
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SendMessageAddress);
                request.Method = "POST";
                request.AllowAutoRedirect = true;
                request.Timeout = 20 * 1000;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Headers.Add("x-cherun-auth-key", "LarxMbndsxfGwoYAqsfJSPPU42l04cb3");
                //string PostData = "a=1&b=2";//请求参数格式
                string PostData = data;//请求参数
                byte[] byteArray = Encoding.Default.GetBytes(PostData);
                request.ContentLength = byteArray.Length;
                using (Stream newStream = request.GetRequestStream())
                {
                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                    newStream.Close();
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream rspStream = response.GetResponseStream();
                using (StreamReader reader = new StreamReader(rspStream, Encoding.UTF8))
                {
                    htmlAll = reader.ReadToEnd();
                    rspStream.Close();
                }
                response.Close();
            }
            catch (Exception ex)
            {

                string s = ex.Message;
            }
            return htmlAll;
        }

 

使用

string data = "appToken=12345258&appKey=dsdsds656s4d6&serviceMethod=gettrack&paramsJson={\"number\":\"" + num + "\"}";
string result = HttpPostFrom(url, data);

 

C# POST application/x-www-form-urlencoded 请求

原文:https://www.cnblogs.com/dyd520/p/14475189.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号