日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
我又踩坑了!如何為HttpClient請求設(shè)置Content-Type標(biāo)頭?

本文轉(zhuǎn)載自微信公眾號「Dotnet Plus」,作者小碼甲 。轉(zhuǎn)載本文請聯(lián)系Dotnet Plus公眾號。 

最近在重構(gòu)認(rèn)證代碼,認(rèn)證過程相當(dāng)常規(guī):

 
 
 
 
  1. POST   /open-api/v1/user-info?client_id&timstamp&rd=12345&sign=***&method=hmac 
  2. content-type: application/json 
  3. payload: { "token":"AA2917B0-C23D-40AB-A43A-4C4B61CC7C74"} 

平臺顯示 :簽名校驗(yàn)失敗, 排查到平臺收到的Post Payload并非預(yù)期,閱讀本文,解鎖正確使用Content-Type標(biāo)頭的姿勢。

1. 入坑

下面是構(gòu)造HttpClient對象、發(fā)起請求的代碼:

 
 
 
 
  1. // 初始化HttpClientFactory 
  2. context.Services.AddHttpClient("platform", c => 
  3.     c.BaseAddress = new Uri("https://alpha-engage.demohost.com/"); 
  4.     c.DefaultRequestHeaders.Accept 
  5.     .Add(new MediaTypeWithQualityHeaderValue("application/json")); 
  6. })... 
  7.  
  8. // 產(chǎn)生命名HttpClient,發(fā)起請求 
  9.  var client = _clientFactory.CreateClient("platform"); 
  10.  var response = await client.PostAsync($"open-api/v1/user-token/info?{req.AuthString()}",new StringContent(req.ReqPayload.ToString(),Encoding.UTF8) ); 

平臺日志顯示,收到的請求payload:

 
 
 
 
  1. {\"token\":\"AA2917B0-C23D-40AB-A43A-4C4B61CC7C74\"} 

額,平臺收到的JSON數(shù)據(jù)被轉(zhuǎn)碼了,沒有識別出JSON?

明眼人一看,HttpClient請求沒有設(shè)置Content-Type,接收端沒有識別出JSON 格式的payload , 進(jìn)行了轉(zhuǎn)碼,生成了錯誤簽名。

① Content-Type是一個Entity Header,指示資源的mediaType ,可用在請求/響應(yīng)中

② 代碼中new StringContent(req.ReqPayload.ToString(),Encoding.UTF8) 沒有指定mediaType參數(shù),故函數(shù)會使用text/plain默認(rèn)值

------------------------------------------

當(dāng)我嘗試添加Content-Type時(下面黃色背景行代碼):

 
 
 
 
  1. context.Services.AddHttpClient("platform", c => 
  2.     c.BaseAddress = new Uri("https://alpha-engage.demohost.com/"); 
  3.     c.DefaultRequestHeaders.Accept 
  4.          .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header 
  5.     c.DefaultRequestHeaders.Add("content-type", "application/json"); 
  6. }) 

此時拋出以下異常:

 
 
 
 
  1. InvalidOperationException: Misused header name. Make sure request headers are used with 
  2. HttpRequestMessage, response headers with HttpResponseMessage, and 
  3. content headers with HttpContent objects.  

納尼,HttpContent Headers是啥?Chrome dev tools顯示只有兩種Header啊?

2. 爬坑

--- 信息 舉例 .NET類型
General Header 可同時作用在請求/響應(yīng)中,但是與傳輸數(shù)據(jù)無關(guān) Upgrade、Connection ---
Request Header 將要獲取的資源或客戶端本身的信息 Accept、
Authorization
HttpRequestHeaders
Response Header 響應(yīng)信息 Location、ETag HttpResponseHeaders
Entity
Header
實(shí)體Body額外的信息 Content-Length、
Connection
HttpContentHeaders

Content-Type屬于Entity Header的一種,對應(yīng).NET類型 HttpContent Header;

雖然Entity Header不是請求標(biāo)頭也不是響應(yīng)標(biāo)頭,它們還是會包含在請求/響應(yīng)標(biāo)頭術(shù)語中(此說法來自官方)。

所以我們在Chrome DevTools沒有看到Entity Headers分組, 卻常在請求/響應(yīng)標(biāo)頭中看到Content-Type標(biāo)頭。

回到上面的異常,.NET 嚴(yán)格區(qū)分四種標(biāo)頭,所以c.DefaultRequestHeaders.Add("content-type", "application/json") 嘗試將content-type添加到請求頭,姿勢不正確,.NET提示InvalidOperationException。

3. 填坑

給這個常規(guī)的Post請求設(shè)置正確的Content-Type標(biāo)頭。

方法① 對HttpRequestMessage對象Content屬性添加Header

 
 
 
 
  1.  using (var request = new HttpRequestMessage()) 
  2.      request.Method = new HttpMethod(method); 
  3.      request.RequestUri = new Uri(url); 
  4.      request.Content = new StringContent(payload); 
  5.      request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
  6.      var response = await _httpClient.SendAsync(request); 
  7.      return response; 

使用HttpClient.SendAsync(request)

方法② 寫入HttpContent時傳入媒體類型

StringContent某個重載構(gòu)造函數(shù) : 參數(shù)3 可直接設(shè)置media type,

 
 
 
 
  1. var response = await client.PostAsync($"open-api/v1/user-token/info?{req.AuthString()}",new StringContent(req.ReqPayload.ToString(),Encoding.UTF8,"application/json") ); 

當(dāng)前文章:我又踩坑了!如何為HttpClient請求設(shè)置Content-Type標(biāo)頭?
當(dāng)前網(wǎng)址:http://www.5511xx.com/article/cosccoh.html