Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

obtaining URL filename

Hi,
I'm working on a java application that needs to retrieve a url's filename (if there is one).
But how do you get the filename of a url like this,
http://www.somesite.com/getfile.php?site=1&file=112&gfile=yes (I've changed the site's name for privacy but it's a real url).
I know it points to a file because it initiates a filedownload and I can retrieve the file contents easily.
I've tried examining the headers for "Content-Disposition" and "Content-Location" but they're not returned by the server. Do I need to send a particular "User-Agent" header to get these values?
The same question also applies to jsp and asp downloads.
0
halogen
Asked:
halogen
  • 3
  • 3
  • 3
  • +3
1 Solution
 
dorwardCommented:
A URL points to a resource, the webserver is under no obligation to link that resource to a _file_, or even suggest a file name.
0
 
tomt84Commented:
If I understand your question right, you can get the original URL from HTTP headers using the PATH_INFO, QUERY_STRING, SCRIPT_NAME, SERVER_NAME, and URL headers. Just try grabbing the contents of those to see which is most suitable. You may find that if you concantate (sp?) some of those strings together you may find what you're looking for. I assume PHP gives you some way to get at the HTTP headers like ASP does (request.servervariables(""))
0
 
mcaxiakCommented:
Well, I hope this helps...
I don't know Java well enough to offer you any help. (Except that in javascript you can go top.location for the address.) Maybe the environment variables used here can be used in the same method in Java.
This does not post. (In order to post I think you may need to use the XML parser and post data to a web site (ughh)).

This is in ASP (could you tell? ;))
<%
Dim FullURL
FullURL = "http://" & Request.ServerVariables("HTTP_HOST") &  Request.ServerVariables("URL")
If Request.ServerVariables("QUERY_STRING") <> "" Then _
     FullURL = FullURL & "?"
FullURL = FullURL & Request.ServerVariables("QUERY_STRING")

Response.Write FullURL
%>

PHP
<?
$full_url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
if ($_SERVER['QUERY_STRING'] != '') {
$full_url = $full_url . '?';
}
$full_url = $full_url . $_SERVER['QUERY_STRING'];

echo $full_url;
?>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
anderson22Commented:
java or jsp/servlets?

Is this a web application?

if this is a web application (such as JSP), you can retrieve the server variables, particularly the SCRIPT_NAMe like tomt84
0
 
halogenAuthor Commented:
Hi,
I think I may have caused some confusion with this question, so I'll try to clarify (although I think dorward is right and there is no consistent method).
My java application is a client program that connects to a web server for the purpose of retrieving a url's contents (in the same way that a web browser would).
If my application wants to save the contents (eg. a file download) it would like to suggest a filename based on information returned by the webserver. In the case of a "normal" url this is simply done by examining the url. But if the server uses asp or php the actual name of the resource is hidden. I know it's possible to retrieve the resource's filename because a web browser like Internet Explorer can do it.
The suggested way to do this is using the "Content-Disposition" header (RFC 2616) but this does not always work.
I'm leaving this question open in the event anyone has an answer.
0
 
anderson22Commented:
This is something that has to be provided by the page (just the mime type, etc.)
0
 
halogenAuthor Commented:
There has not been a satisfactory answer to this question.
As a result I would like to close this thread.
0
 
dorwardCommented:
Post a message in community support then.
0
 
moduloCommented:
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20557547.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0
 
tomt84Commented:
I believe that there are two issues here:

1) The question was slightly ambiguous

2) The answers provided by myself, anderson22, and mcaxiak demonstrate at least 3 different ways to accomplish the task set, i.e. the original URL of a request can be provided by one or more different http headers strung together. If this is not what the question author was looking for then my point #1 is supported. If this solution is what the author required then I believe s/he wants the points refunding after having gained an acceptable pointer in the right direction if not a direct answer. Maybe if the author could submit some clarification and/or source code then we would be better able to work something out.

Regards
Tomt84
0
 
halogenAuthor Commented:
tomt84,
As I tried to clarify, this is not a server app, nor is it an applet that runs within a browser, therefore the answers provided are not appropiate to this question. The headers that you mentioned are PHP variables that can be set to send information to the client, there is no way for the client to access these values except as standard http headers eg "Content-Type", my problem was that these headers were not always being sent by the server.
0
 
anderson22Commented:
If you use the HTTP protocol, standards implemented by W3C propose that headers are a standard for communication.  No application must follow these guidelines, but they are not compliant if they do not.

Now, if you are using regular socket (client/server) communication. There is no definition that says a header or any information is required.

HTTP is a protocol with a defined structure.

-rca
0
 
moduloCommented:
Question looks active again.
Will check for proposal how to close this Q after 72 hrs.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0
 
moduloCommented:
Saving this Q as a PAQ and refunding the 20 points to the questionner

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now