zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    mysql修改表
    MySQL sql优化(摘抄自文档)
    mysql show操作
    mysql load data infile
    mysql Insert强化
    mysql group_concat
    HTML js 复习
    mysql开发实战8问
    不使用Ajax,如何实现表单提交不刷新页面
    跨域的方式总结
  • 原文地址:https://www.cnblogs.com/dyd520/p/14475189.html
Copyright © 2011-2022 走看看