java中使用socket的多个HTTP请求
我如何使用套接字从我的 java 程序发送多个 http 请求。实际上我已经尝试过:
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){
}
}
}
但是当我再次发送请求时它没有被处理? 提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想使用 HttpURLConnection 代替。它抽象了很多 HTTP 细节,包括连接管道。
You want to use HttpURLConnection instead. It abstracts a lot of HTTP details, including connection pipelining.
我不清楚您为什么尝试使用套接字发送请求。但您可能想使用 apache HttpClient 来发送请求。示例可以在这里找到: http://hc.apache.org/httpclient-3 .x/tutorial.html
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