[英].NET - Content-Disposition Header Not Showing in WebAPI
I'm trying to create a WebAPI that will make a GET request to another server (enterprise Oracle Agile PLM), and return a document. 我正在尝试创建一个WebAPI,该WebAPI将向另一个服务器(企业Oracle Agile PLM)发出GET请求,并返回一个文档。 I'm using .NET 4.5.1. 我正在使用.NET 4.5.1。 I've been able to get the actual file data, but having a hard time getting the original filename for it. 我已经能够获取实际的文件数据,但是很难获取其原始文件名。 I've read that the Content-Disposition header of the response is the best place to look for this, as it isn't included in the URI. 我读到响应的Content-Disposition标头是查找此内容的最佳位置,因为它不包含在URI中。
Here's what I'm currently trying and failing with. 这是我目前正在尝试并失败的内容。 This is way incomplete now that I'm purely focused on trying to get the header to show: 既然我只专注于尝试显示标题,这是不完整的:
public string GetDocsForDocNumber()
{
var docUrlString = "https://agprd.myDomain.com/Agile/link/Information%20Systems/1044698/files/ALL";
var username = "user";
var password = "pass";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(docUrlString);
try
{
string authKey = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + authKey);
//DEBUG - Printing out all request headers...
WebHeaderCollection whCollection2 = request.Headers;
for (int i = 0; i < whCollection2.Count; i++)
{
System.Diagnostics.Debug.WriteLine("\n REQUEST header key: " + whCollection2.GetKey(i) + " value: " + whCollection2.Get(i));
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream rstream = response.GetResponseStream())
{
//DEBUG -- Printing out all response headers...
WebHeaderCollection whCollection = res.Headers;
for (int i = 0; i < whCollection.Count; i++)
{
System.Diagnostics.Debug.WriteLine("header key: " + whCollection.GetKey(i) + " value: " + whCollection.Get(i) + "\n");
}
}
res.Close();
}
catch { }
return "blah";
}
Here are the headers that get printed in VS: 这是在VS中打印的标题:
REQUEST header key: Authorization value: Basic eWFAKeJaSEHnef2jk9hasE= (this is fudged FYI)
RESPONSE header key: Transfer-Encoding value: chunked
RESPONSE header key: Cache-Control value: private
RESPONSE header key: Content-Type value: application/octet-stream
RESPONSE header key: Date value: Fri, 28 Sep 2018 18:38:15 GMT
RESPONSE header key: Set-Cookie value: JSESSIONID=GTF8bn1XvKPw1mQ1HJaejfejjaE3fNb0pTG5T15VVbwGlQ!331878993!-1921346355; path=/; HttpOnly,NSC_WTWS-BHQSE-443=ffffffffaf1cef7b419874afe8455e445a4a422d69;Version=1;path=/;secure
RESPONSE header key: X-Powered-By value: Servlet/3.0 JSP/2.2
I've tried similar things using WebClient, HTTPClient, etc and can't get this to show up. 我已经尝试使用WebClient,HTTPClient等进行类似的操作,但无法显示出来。 If I take that URI and plug it into my browser, here's what the response header shows: 如果我使用该URI并将其插入浏览器,则响应标头显示如下:
Browser response header pic 浏览器响应标题图片
I don't understand why it's not there when I make the request from the WebAPI. 当我从WebAPI发出请求时,我不明白为什么它不存在。 I can make the same request from an iOS app and it works fine. 我可以从iOS应用程序发出相同的请求,并且效果很好。 Again, I can get the actual document data just fine, but I'm not able to get the filename because that header is MIA. 同样,我可以得到实际的文档数据,但是我无法获得文件名,因为该标头是MIA。
Any ideas? 有任何想法吗? I'm fairly new to this, so I'm hoping it's something dumb and easy. 我对此还很陌生,所以我希望它既简单又简单。
Thanks 谢谢
The oracle server is only sending the content-disposition
when there is a valid user-agent
present (browsers automatically send a user-agent header). oracle服务器仅在存在有效的user-agent
时发送content-disposition
(浏览器会自动发送用户代理标头)。 The oracle server probably does this because it assumes that you wouldn't want the content-disposition
header unless you are a browser (browsers use the content-disposition
header to know how to treat certain data coming back. ie download this file, display this image, etc.). oracle服务器可能会这样做,因为它假设除非您是浏览器,否则您将不希望content-disposition
标头(浏览器使用content-disposition
标头来知道如何处理返回的某些数据。即下载此文件,显示此内容。图片等)。
So add the user-agent
header yourself to get the content-disposition
header back. 因此,您可以自己添加user-agent
标头,以获取content-disposition
标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.