C# 创建json传输格式的http请求

public static string PostRequestTest(string content, string url, string contentType = "application/json")
   {
       try
       {
           var memStream = new MemoryStream();
           var cc = Encoding.UTF8.GetBytes(content);
           var request = (HttpWebRequest)WebRequest.Create(url);
           request.Method = "POST";
           request.ContentType = contentType;
 
           memStream.Write(cc, 0, cc.Length);
 
           request.ContentLength = memStream.Length;
 
           var requestStream = request.GetRequestStream();
 
           memStream.Position = 0;
           var tempBuffer = new byte[memStream.Length];
           memStream.Read(tempBuffer, 0, tempBuffer.Length);
           requestStream.Write(tempBuffer, 0, tempBuffer.Length);
           requestStream.Close();
 
           string responseTxt = string.Empty;
 
           WebResponse response = request.GetResponse();
           using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) //Encoding.GetEncoding("gb2312")
           {
               responseTxt = reader.ReadToEnd();
           }
           response.Close();
 
           return responseTxt;
       }
       catch (Exception ex)
       {
           throw new Exception(ex.Message);
       }
   }

转载于:https://www.cnblogs.com/fanying/p/10918962.html

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页