karry
karry

Reputation: 3410

Getting Filename from virtual URL?

I have a URL like "http://www.ti.com/lit/gpn/TPS767D318-Q1" which is a path eventually being routed to "http://www.ti.com/lit/ds/symlink/tps767d318-q1.pdf" on the browser(rendering a pdf file). I am processing this URL in a Console application in order to fetch the "pdf" filename that you see in the second URL i provided.

I checked the UriResponse.Absoluteuri property in the httpresponse object and it says "http://focus.ti.com/general/docs/lit/getliterature.tsp?genericPartNumber=TPS767D318-Q1&fileType=pdf" looks like this is a nested virtual path. Can anybody help on where i can get to the end URL to extract the pdf file name? i did not find it anywhere in the response object. I also checked in the Response headers and nothing there either.

Any help will be appreciated...Thanks

Upvotes: 1

Views: 267

Answers (3)

karry
karry

Reputation: 3410

It turns out that the URL in my question is actually returning HTML content and doing a "meta tag" redirect. So I had to do the following:

var redirect = Regex.Match(new string(buffer, 0, count), @"\<meta(?=[^>]*http-equiv\W*refresh)[^>]*?content\s*\=[^=>]*url\s*\=\s*(?<Url>[^'"">]+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

if (redirect.Success)
{
    Uri uri = new Uri(new Uri(externalUrl, UriKind.Absolute), new Uri(redirect.Groups["Url"].Value, UriKind.RelativeOrAbsolute));
    return SaveUrlToTemporaryFile(uri.AbsoluteUri, needsFullDownload);
 }

I'm getting the final URL out of the meta tags from the returned HTML content, and calling my download routine again.

Upvotes: 1

CodingGorilla
CodingGorilla

Reputation: 19842

Look at the Content-Disposition header, it might look something like: Content-Disposition: attachment; filename=tps767d318-q1.pdf. This is a common technique for webservices that fetch and "download" files from database, network shares, etc.

Upvotes: 1

PlexQ
PlexQ

Reputation: 3110

Not sure about ASP, but at the protocol level the initial request may cause a Redirect to be issued by the application/server on the other end, so you can look at the initial HTTP response and check if it's a redirect code, 301, 302 etc. If so, you can follow the 302s until you hit a 200, and that's the final URL you can use to check the filename.

Upvotes: 1

Related Questions