HttpWebRequest Headers与StreamReader 顺序问题

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
request.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"xxx:xxx"))}");

StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(postDataStr);
writer.Flush();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8";
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
JObject jo = (JObject)JsonConvert.DeserializeObject(retString);
var data = getValue(jo, "DATAS");

 

 

StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);要写在


request.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"xxx:xxx"))}");

后面

.net core中顺序随意,framework中不行记录一下。

posted on 2022-06-29 16:06  愤怒的小雀  阅读(50)  评论(0编辑  收藏  举报