2016-01-14 78 views
4

場景:
首先,請考慮我使用HttpClient類,而不是WebRequest,我知道有很多相關的問題這裏所有的答案都是針對WebRequest的。無法獲取標題「內容處置」與HttpClient的在C#

我做了,以這種方式設置以下標題以下載一個.txt文件的Web API應用程序:

resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain"); 
resp.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") 
{ 
    FileName = _datacontext.GetAppConfig("814FileNameHeader") + DateTime.Now.ToString("yyyyMMdd") + ".txt" 
}; 
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 

後,我做了一個HttpClient的其他應用程序連接到這個方法。
一切正常,檢索200和文件的內容。但是爲了讀取文件名,我無法獲取標題。

TRIES MADE:

內容處置::

與REST客戶端可以閱讀下面的頭屬性工作附件;文件名= 814Entes_PG_20160114.txt

,並從代碼我可以調試它,我發現,標題是「invalidHeaders」但我不能達到的值:

enter image description here

問題:

我該如何設置這個並毫無疑問地從客戶端獲得?

UPDATE:

這些都是我試圖讓頭: 原:

   using (var httpClient = new HttpClient()) 
      { 
       httpClient.BaseAddress = new Uri(uri); 
       string authentication = string.Concat(authenticationArgs[0], ":", authenticationArgs[1]); 
       httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic " + Base64Encode(authentication)); 
       httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
       HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "Report/" + type); 
       req.Content = new StringContent(""); 
       HttpResponseMessage response = await httpClient.SendAsync(req); 
       Console.WriteLine(response.StatusCode); 
       if (response.IsSuccessStatusCode) 
       { 
        string stream = response.Content.ReadAsStringAsync().Result; 
        Console.Write("Respuesta servicio: " + stream); 
        Console.WriteLine(stream); 

        string cp = response.Headers.GetValues("Content-Disposition").ToString(); 
       } 

第二個我有一些所謂的調查試過:

 using (var httpClient = new HttpClient()) 
     { 
      httpClient.BaseAddress = new Uri(uri); 
      string authentication = string.Concat(authenticationArgs[0], ":", authenticationArgs[1]); 
      httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic " + Base64Encode(authentication)); 
      httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "Report/" + type); 
      req.Content = new StringContent(""); 
      HttpResponseMessage response = await httpClient.SendAsync(req); 
      Console.WriteLine(response.StatusCode); 
      if (response.IsSuccessStatusCode) 
      { 
       string stream = response.Content.ReadAsStringAsync().Result; 
       Console.Write("Respuesta servicio: " + stream); 
       Console.WriteLine(stream); 
       string cp = ""; 
       HttpHeaders headers = response.Headers; 
       IEnumerable<string> values; 
       if (headers.TryGetValues("Content-Disposition", out values)) 
       { 
        cp = values.ToString(); 
       } 

      } 
+2

您可以顯示發出請求的代碼以及如何嘗試「獲取」標題嗎?鑑於內容長度和內容類型也在「無效頭」集合中,我會懷疑你的請求代碼。 – CodeCaster

+0

當然,我不認爲這是,讓我更新我的問題。完成@CodeCaster感謝您的建議! –

+0

只是好奇,但如果MediaTypeHeaderValue是'application/octet-stream',你不應該用ReadAsStreamAsync()讀取響應。 – terbubbs

回答

9

如果您試圖獲取內容的標題,它應該是fr om response.Content.Headers