zoukankan      html  css  js  c++  java
  • C# POST请求 json格式

       /*
        *  url:POST请求地址,例如:url = "http://localhost:35229/ddn/GetPostData";
        *  postData:json格式的请求报文,例如:{"key1":"value1","key2":"value2"}
        */
    
         public static string PostUrl(string url, string postData)
            {
                string result = "";
    
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    
                req.Method = "POST";
    
                req.TimeOut = "800";//设置请求超时时间,单位为毫秒
    
                req.ContentType = "application/json";
    
                byte[] data = Encoding.UTF8.GetBytes(postData);
    
                req.ContentLength = data.Length;
    
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
    
                    reqStream.Close();
                }
    
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    
                Stream stream = resp.GetResponseStream();
    
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
    
                return result;
            }
  • 相关阅读:
    jquery height
    正则表达式的一点奇怪
    this和call
    ajax views
    史上变态的模块
    在php中有什么用
    localhost访问不了
    $.extend abc
    $.extend
    和人沟通的一个要点
  • 原文地址:https://www.cnblogs.com/jiangshuai52511/p/9377366.html
Copyright © 2011-2022 走看看