0

I was searching some examples about sending HTTP requests in order to display web page on my browser by using java. I couldn't find a simple example for that. Do you have any suggestions ? Where should I look for a good example with explanations ? Thank you

Ahmet Tanakol
  • 849
  • 2
  • 20
  • 40
  • 3
    Your question is not clear. Do you mean that you want to create java application that runs on client side, opens user's default browser and shows web page in it? – AlexR Oct 11 '12 at 16:37
  • For now yes, I just want to see basics of http requests and how to display a web page on my browser by sending http request. As you said it is a java app and runs on client side. – Ahmet Tanakol Oct 11 '12 at 16:44

2 Answers2

1

You can use java.net.URL, java.net.URLConnection, java.io.InputStream, org.apache.commons.io.IOUtils to manage HTTP requests in a java application

Here is an example class -

public class HttpUtil
{
    static URL url;
    static URLConnection urlConn;
    static DataOutputStream out;
    static BufferedReader input;

    static public String get(String _url)
    {
        try 
        {
            url = new URL(_url.replace(" ", "%20"));

            InputStream input = url.openStream();
            StringWriter writer = new StringWriter();
            IOUtils.copy(input, writer);
            return writer.toString();

        }
        catch (Exception e)
        {
            return "ERROR: " + e.getMessage();
        }
    }

    static public String post(String _url, String postData)
    {
        String result = "";

        try
        {
            url = new URL(_url);

             urlConn = url.openConnection();
             urlConn.setDoInput(true);
             urlConn.setDoOutput(true);
             urlConn.setUseCaches(false);
             urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

             out = new DataOutputStream(urlConn.getOutputStream());
             String content = postData;

             out.writeBytes(content); // send the data

             out.flush();
             out.close();

             DataInputStream in = new DataInputStream(urlConn.getInputStream());
             input = new BufferedReader(new InputStreamReader(in));

             String str;

             while ( (str = input.readLine() ) != null )
             {
                 result = result + str + "\n";
             }

             input.close();
        }
        catch (Exception e) 
        {
            System.err.println(e.toString());
            return null;
        }

        return result;
    }
}

You can also fire up a web browser in java by using this little snippet -

URI url = new URI("file:/" + ur); // or an absolute path to a website http://google.com/

Desktop.getDesktop().browse(url);
ddavison
  • 28,221
  • 15
  • 85
  • 110
  • org.apache.commons.io.IOUtils cannot be resolved, I think it can be because of my eclipse version. I'm using Juno – Ahmet Tanakol Oct 11 '12 at 16:48
  • 1
    If you aren't using Maven, download it from http://commons.apache.org/io/download_io.cgi and add the .jar file the download comes with, to your build path. If using Maven, this might help - http://mvnrepository.com/artifact/commons-lang/commons-lang/2.2 – ddavison Oct 11 '12 at 16:55
0

Use

Desktop.browse("http://stackoverflow.com/questions/12844363/displaying-a-web-page-with-http-request");

See Also

jmj
  • 237,923
  • 42
  • 401
  • 438
  • The `java.awt` package never stops to surprise me. (http://stackoverflow.com/q/58305/1374678) – rolve Oct 11 '12 at 16:41