views:

20733

answers:

6

I want to compose a HTTP request message in java and then want to send it to a HTTP WebServer. I also want the document content of the page recieved which I would have recieved if I sent the same HTTP request from a webpage.

+11  A: 

You can use java.net.HttpUrlConnection.

You can find sample code snippets at Java Almanac. Browse for java.net.

duffymo
A very thanks to you. The site address you provided solved my problem.Have a good day...
Yatendra Goel
You're very welcome. Java Almanac is worth knowing about.
duffymo
+8  A: 

From Sun's java tutorial

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}
Chi
+1  A: 

http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html

In particular, getHeaderField, getHeaderFieldKey, and getContent

klez
+5  A: 

I know others will recommend Apache's http-client, but it adds complexity (i.e., more things that can go wrong) that is rarely warranted. For a simple task, java.net.URL will do.

URL url = new URL("http://www.y.com/url");
InputStream is = url.openStream();
try {
  /* Now read the retrieved document from the stream. */
  ...
} finally {
  is.close();
}
erickson
That doesn't help if you want to monkey with request headers, something that's particularly useful when dealing with sites that will only respond a certain way to popular browsers.
Jherico
You can monkey with request headers using URLConnection, but the poster doesn't ask for that; judging from the question, a simple answer is important.
erickson
+8  A: 

Apache HttpComponents. The examples for the two modules - HttpCore and HttpClient will get you started right away.

Not that HttpUrlConnection is a bad choice, HttpComponents will abstract a lot of the tedious coding away. I would recommend this, if you really want to support a lot of HTTP servers/clients with minimum code. By the way, HttpCore could be used for applications (clients or servers) with minimum functionality, whereas HttpClient is to be used for clients that require support for multiple authentication schemes, cookie support etc.

Vineet Reynolds
A: 

I think I do not have proper privilege to comment on posts here. But links in this response does not work the correct example URLs are Apache HttpClient and Apache HttpCore

I guess mods can update it.

Nishant
You can comment when you have 50+ reputation and you can edit other's posts when you have 2000+ reputation. See also http://stackoverflow.com/faq
BalusC
Yeah, it's good if someone with 2k+ updates that answer with correct URLs in my response.
Nishant