Newbie question.. reading location header

J

john

Hi, i'm new to java, I'm trying to read the "location header" that
contains webpage redirect information from a web server. What class do
i use? can you provide an example?

Thanks in advance.
 
R

Rhino

john said:
Hi, i'm new to java, I'm trying to read the "location header" that
contains webpage redirect information from a web server. What class do
i use? can you provide an example?
That's pretty hard to answer as you've given us very little information. It
would help to know what kind of program you are writing: an applet? an
application? a servlet? a midlet? something else?

It should be fairly easy to get that information in a servlet but most Java
newbies don't start by writing servlets. Other types of Java programs may or
may not have access to that information. It's not reasonable to ask us to
list all the classes and methods that might do the job without any idea of
what you're doing.

Rhino
 
J

john

Hi Rhino, i'm trying to write an application. I use the following
code:
import java.net.*;
import java.io.*;
public class Testing {

public static void main(String[] args) {
{
try {
URL u = new
URL("http://www.techbargains.com/jump.cfm?id=75&arg=retail/product.asp?sku=10330420");
URLConnection newu = u.openConnection();
for(int i=0;i<11;i++){
String x=newu.getHeaderField(i);

System.out.print("--------------------------------------------"+'\n');
System.out.println("field number: " + i);
System.out.println(x);
}

}
catch (IOException e) {
System.err.println(e);
}
}
}
}

The code returns some server info, but no rediect info, such as the
redirected link.

Thanks
 
J

john

Yes, but that is not what i need. Specifically, i need to read the
redirected link (the URL) from the server. I was really suprised
there aren't much info out there on the internet, I have search google
and the news group, but couldn't find much anything.

Thanks,
 
T

Thomas Hawtin

john said:
Yes, but that is not what i need. Specifically, i need to read the
redirected link (the URL) from the server. I was really suprised
there aren't much info out there on the internet, I have search google
and the news group, but couldn't find much anything.

What don't you need?

I believe I gave you a link to the method in HttpURLConnection that
controls whether redirects are followed or not.

Tom Hawtin
 
J

john

thanks for your response Thomas, you did give me the link to the
HttpURLConnection class, maybe i just have learn how to apply it. You
are great help!!!
 
R

Roedy Green

I believe I gave you a link to the method in HttpURLConnection that
controls whether redirects are followed or not.

If you DON'T follow redirects, you would get the web page containing
the redirect, would you not, which you could study.

Or are these redirects done without an intermediate web page?
 
C

Chris Smith

john said:
I have tried to use HttpURLConnection, but still couldn't be able to
get the redirected
link(((http://www.techbargains.com/jump.cfm?id=75&arg=retail/product.asp?sku...
====>www.sitename.com/oiwoowl.....)))). I just need the limk, not
the webpage itself. There seem not way of doing it in Java.

Of course there's a way of doing it in Java. You are doing something
wrong. I don't know what, because you've told us very little about what
you're doing in the first place. "still couldn't be able to get the
redirected link" doesn't communicate much about how you tried to do it.
Did you try calling getHeaderField on the HttpURLConnection after making
the request?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Joined
Feb 10, 2011
Messages
1
Reaction score
0
Hi..
Bumping a very old thread.... but i guess i also need the same information...

I am using HttpURLConnection and am able to connect to an URL, submit a form and load the resulting page successfully.
But the problem is i am not interested in the resulting page, but the resulting URL that i would have seen in the address bar, if i were to do the same operations manually.

I hope someone can help me in achieving this.

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top