1

How could i send multiple http requests from my java program using sockets. actually i have tried as:

import java.net.*;
import java.io.*;
class htmlPageFetch{
    public static void main(String[] args){
        try{
            Socket s = new Socket("127.0.0.1", 80);
            DataInputStream dIn = new DataInputStream(s.getInputStream());
            PrintWriter dOut = new PrintWriter(s.getOutputStream(), true);
            dOut.println("GET /mytesting/justCheck.html HTTP/1.1\r\nHost:localhost\r\n\r\n");
            boolean more_data = true;
            String str;
            int i = 0;
            while(more_data){
                str = dIn.readLine();
                if(str==null){
                    //Now server has stopped sending data               //So now write again the inputs
                    dOut.println("GET /mytesting/justCheck1.html HTTP/1.1\r\nHost:localhost\r\n\r\n");                          


                    continue;
                }
                System.out.println(str);
            }   
        }catch(IOException e){

        }
    }
}

But when I send the request again it was not processed? Thank in advance.

4
  • You shouldn't catch an exception and do nothing with it. In your exception, at least print the stacktrace (e.printStackTrace()) to see if there's an error. Commented Dec 22, 2010 at 14:35
  • 1
    Do you have to do this with sockets ? HTTP is abstracted out in several libraries that would clean this up some. Commented Dec 22, 2010 at 14:49
  • 1
    Another hint: do not generate HTTP packets or any other protocol packets inline. There are LOADS of libraries for that. For example Apache HttpClient (hc.apache.org/httpclient-3.x/features.html) Commented Dec 22, 2010 at 14:50
  • Hey thanks to all you but can't i do with sockets?? Commented Dec 28, 2010 at 12:09

2 Answers 2

3

You want to use HttpURLConnection instead. It abstracts a lot of HTTP details, including connection pipelining.

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

Comments

0

I'm not clear that why you are trying to send request using socket. But you might want to use apache HttpClient to sending the request. Sample can be found here: http://hc.apache.org/httpclient-3.x/tutorial.html

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.