Skip to Main Content

Java Programming

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 set http headers properties?

807591Jun 16 2008 — edited Jun 16 2008
Method one -
sendMessage = "GET /path/example.html HTTP/1.1\r\n" +
                           "Host: www.somesite.com\r\n" +
                           "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071220 BonEcho/2.0.0.11\r\n" +
                           "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
                           "Accept-Language: en-us,en;q=0.5\r\n" +
                           "Accept-Encoding: gzip,deflate\r\n" +
                           "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
                           "Keep-Alive: 300\r\n" +
                           "Referer: http://www.originalsite.com/\r\n" +
                           "Connection: keep-alive\r\n\r\n"

url = "www.somesite.com";
port = 80;
sock = new Socket(url, port);
request = sock.getOutputStream();
request.write(sendMessage.getBytes());
Method two -
URL url = new URL("http://www.somesite.com");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
Can I use method two to do the same thing like method one?
Method one - I can make the sendMessage headers whatever I want.
Method two - what are the methods for "GET /path/example.html HTTP/1.1", "Referer: http://www.originalsite.com/", etc.,
By the way, can I repeat using setRequestProperty to set the other headers as below?
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071220 BonEcho/2.0.0.11");
conn.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "300");
conn.setRequestProperty("Connection", "keep-alive");
Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2008
Added on Jun 16 2008
1 comment
732 views