开发者

Multiple HTTP requests using sockets in java

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 i开发者_Python百科nputs
                    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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜