Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to add custom headers to a request.

843790Jul 1 2009 — edited Jul 2 2009
Hi!

I'm trying to get the source of a website, and I'm doing that successfully.

The only thing is i need to be able to set the user-agent header in the request to the site.

This is my current code (slightly modified) to get the source from the page.
URL url = new URL("http://www.mycustomsite.com/page.html");
inputStream = new BufferedInputStream(url.openStream());
DataInputStream in = new DataInputStream(inputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = new String();
String file = new String();

try {
while((line = br.readLine()) != null){
file += line+  "\n";
}
}catch(Throwable e){
  System.out.println("Could not read line");
}
Writer output = new BufferedWriter(new FileWriter("C:/my_web_page.html"));
output.write(file); 
What do i have to do to tell Java to add the user-agent header into request?

(It doesn't matter to me if i have to rewrite the whole thing, i just wanna know how)

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 30 2009
Added on Jul 1 2009
1 comment
1,470 views