3

I'm using this following code to send simple HTTP Request :

try
{
    Socket  s = new Socket ();
    s.bind    (new InetSocketAddress (ipFrom, 0));
    s.connect (new InetSocketAddress (ipTo,   80), 1000);

    PrintWriter     writer = new PrintWriter    (s.getOutputStream ());
    BufferedReader  reader = new BufferedReader (new InputStreamReader (s.getInputStream ()));

    writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n"); 
    writer.flush ();

    s     .close ();
    reader.close ();
    writer.close ();
}

However, as you can see, I don't send a custom HEADER. What should I add to send a custom HEADER ?

6 Answers 6

6

Don't try to implement the HTTP protocol yourself.

Use HttpComponents by Apache.

(or its older and a little easier to use version - HttpClient)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi. Thanks for this advice. It seems to be very simple and powerful !
5

When you write

writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n"); 

The \r\n\r\n bit is sending a line-feed/carriage-return to end the line and then another one to indicate that there are no more headers. This is a standard in both HTTP and email formats, i.e. a blank line indicates the end of headers. In order to add additional headers you just need to not send that sequence until you're done. You can do the following instead

writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
writer.print ("header1: value1\r\n"); 
writer.print ("header2: value2\r\n"); 
writer.print ("header3: value3\r\n"); 
// end the header section
writer.print ("\r\n"); 

Comments

1

Even if I suggest to try HttpComponents as mentioned by Bozho instead of implementing HTTP by yourself, this is would be the way to add a custom header:

 writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
 writer.print ("X-MyOwnHeader: SomeValue\r\n");

2 Comments

Specifically, the end of the header section is denoted by "\r\n\r\n", so whatever the last header line is should end in that, or should be followed by another line saying writer.print("\r\n");
Almost right, you just need another writer.print("\r\n"); at the end to finish the header part.
1

You should use classes already prepared to be used for http connections, like HTTPUrlConnection that is a childreon of UrlConnection and has this method

void setRequestProperty(String key, String value)

that should be used to set parameters of the request (like HEADER field).. check here for reference

1 Comment

Not if your intent is to learn a lower level abstraction of computer science.
1

You can also see URLConnection.

https://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html

2 Comments

Hi, I can't really use URLConnection because I need to bind to a local IP. My server have 5 ips, and I need to send my Request from on of this.
@billOute - what? A URLConnection is perfectly capable of dealing with that. You just need to use different IP addresses in the URLs!!
0

If you absolutely have to do it yourself by hand it must follow this format with each header on its own line.

name: value

Look into the header format in HTTP spec.

http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#Message-Headers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.