Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a custom user agent for java - possible?

Status
Not open for further replies.

ifx

Technical User
Feb 26, 2002
57
GB
Hi,

I'm developing a wbe crawler in Java and have got a reasonable way, with the exception of setting the user agent. Currently it seems to be "Java1.3.0". Here's a snippet of code I'm using if it's any help:

URLConnection urlConnection = url.openConnection();
System.out.println("Downloading " + url.toString());

urlConnection.setAllowUserInteraction(false);

InputStream urlStream = url.openStream();

Is there any way of setting the user agent?

Thanks in advance if you can help with this!
 
Set the USER-AGENT HTTP header. That's all there is to it.
see:
[blue]
URLConnection.setRequestProperty()
[/blue]

-pete
 
Thanks for your help (sorry for not getting back sooner). It all compiles and runs but doesn't seem to have any impac on the user agent. Is there something I've overlooked? The UA is still Java1.3.0. Below is a of code where the url is opened, is there anything I've missed?:

public String getpage(URL url)

{ try {
// try opening the URL
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent","MyAgent");
urlConnection.setAllowUserInteraction(false);

System.out.println("Downloading " + url.toString());
InputStream urlStream = url.openStream();
...

}

Thanks a lot for your time!
 
Just in case anyone is curious, I did some digging and found that while setRequestProperty() is a valid method, it doesn't actually do anything! It was created but never fully implemented. Even in 1.4 it doesn't seem to work. Oh well...!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top