简体   繁体   中英

.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. I'm using .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.

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:

 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. If I take that URI and plug it into my browser, here's what the response header shows:

Browser response header pic

I don't understand why it's not there when I make the request from the WebAPI. I can make the same request from an iOS app and it works fine. Again, I can get the actual document data just fine, but I'm not able to get the filename because that header is 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). 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.).

So add the user-agent header yourself to get the content-disposition header back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM