• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling Redirects

 
Steve Schooler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the right sub-forum of Java Ranch for this post??

Trying to create java application that will download a file from a website (e.g. www.website1.com). Following excerpt generally works ok.



Unfortunately, www.website1.com redirects to www.website2.com, so huc.getResponseCode() <above> returns 302 rather than 0. I tried inserting



but still get 302 rather than 0. How do I supply my UserID/Passwd to handle the redirect?

More generally, I'd like to not have to supply the UserId/Passwd with each GET request. Instead, I want to (somehow) login to website1 or website2 at the start of the app (only). Is this doable?

As a last resort, could I create an applet to download the file. From Firefox, if I login to website1 (thru website2), then any other "tabs" in that Firefox session are "authorized". Any reason why this approach won't work?

Request insights, coding samples, website references...
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I am not on a computer where I can test what I am going to suggest. But this might help you.

First, the authentication must be handled by a java.net.Authenticator.

See this example in the Java Alamanc 1.4

Now, regarding the redirect, the java.net.HttpURLConnection have a method setFollowRedirects() that sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.

Maybe you can turn on this flag and see what happens.



I hope this helps!
[ July 25, 2006: Message edited by: Edwin Dalorzo ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need to use base-64 encoding with the username and password. Look here for an example.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!