posted 11 years ago
Hello All at the Ranch:
I have a simple question regarding urlConnection.getInputStream();
Referring to the bit of code below, my question is when does the data
come down from the remote server. When I call getInputStream?
Do the full results get sent at that time?
Or somewhere in the while loop?
If I check result.available() immediately after getInputStream() I get 0 zero.
Thought I would need to know this to put some error handling around this
code.
thanks for your kind assistance.
KD
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout( 30 * 1000 ); // wait 30 seconds to connect
urlConnection.setReadTimeout( 120 * 1000 ); // wait 120 seconds for data to return
urlConnection.setRequestProperty("Proxy-Authorization", encodedPword);
InputStream result = urlConnection.getInputStream();
BufferedInputStream bis = new BufferedInputStream( result, 4096);
StringBuffer strBuffer = new StringBuffer();
/* read the InputStream into a StringBuffer. Change code here to handle result set as desired*/
while (true) {
byte buffer[] = new byte [4096];
int endOfResultSet = bis.read (buffer, 0, 4096);
if (endOfResultSet == -1)
break;
for (int i=0; i<buffer.length; i++) {
strBuffer.append ((char) buffer[i]);
System.out.print( (char) buffer[i]);
}
System.out.println("\n** ** strBuffer len: " + strBuffer.length() );
//Thread.sleep(500);
} //while