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

 

 

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 请求

上一篇:Android - 利用扩展函数为Bitmap添加文字水印


下一篇:uni-app离线打包和云打包