Reputation: 35
I am trying to create a program that will get data directly from socket instead of going through using HttpURLConnection or HttpClient. Using a socket will give me more freedom to manipulate the data to match the requirements. I am hoping that using sockets, I can preserve the chunk headers sent with each chunk. Following is my code to accomplish that. Unfortunately, even though the code runs without any errors, it runs for at least 40 seconds before it stops. Also, I don't get any InputStream from the server even though I checked that the program was connected to the server.
Socket socket = new Socket(hostAddr, port);
InputStream in = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
char[] streamBuff = new char[8192];
StringBuilder receivedData = new StringBuilder();
int count = 0;
while((count = isr.read(streamBuff)) > 0)
{
receivedData.append(streamBuff, 0, count);
}
System.out.println(receivedData);
Upvotes: 1
Views: 1907
Reputation: 33534
You first need to make a request, either GET or POST. Here's an example to show how to do use openStream()
and then read the InputStream
:
public class DateWala {
public static void main(String[] args){
try {
URL url = new URL("http://www.google.com");
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s=br.readLine())!=null){
System.out.println(s);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 84169
HTTP/1.1 has persistent connections. You are reading all response chunks in that loop and then block until server times out and closes you TCP connection.
Upvotes: 1