码迷,mamicode.com
首页 > Web开发 > 详细

HttpWebRequest 自定义header,Post发送请求,请求形式是json,坑爹的代码

时间:2018-12-15 10:25:17      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:[]   sts   close   item   static   create   tco   res   坑爹   

public static string PostMoths(string url, LoginDTO obj_model, Dictionary<string, string> dic = null)
{
dic = new Dictionary<string, string>();
dic.Add("Abp.TenantId", "null");
// .AspNetCore.Culture:zh - CN
dic.Add(".AspNetCore.Culture", "zh-CN");
string param = JsonConvert.SerializeObject(obj_model);
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
if (dic != null && dic.Count != 0)
{
foreach (var item in dic)
{
request.Headers.Add(item.Key, item.Value);
}
}
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(param);
request.ContentLength = payload.Length;
string strValue = "";
try
{
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate;
}
}
catch (Exception e)
{
strValue = e.Message;
}
return strValue;

HttpWebRequest 自定义header,Post发送请求,请求形式是json,坑爹的代码

标签:[]   sts   close   item   static   create   tco   res   坑爹   

原文地址:https://www.cnblogs.com/topguntopgun/p/10122345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!