HttpUrlConnection => reading all packets

T

Terrie

Dear all,

in my project I have to contact a server and read a big amount of byte
data. I am using HTTPUrlConnection and have to use POST. All works
fine but when the amount of data is bigger then one packet size (appr.
1450 Byte) then the rest of the data is omitted. Does anybody know how
to read ALL packets that are send vie HTTP Post??

Any ideas?

Thx,

Sabine
 
M

Murray

Terrie said:
Dear all,

in my project I have to contact a server and read a big amount of byte
data. I am using HTTPUrlConnection and have to use POST. All works
fine but when the amount of data is bigger then one packet size (appr.
1450 Byte) then the rest of the data is omitted. Does anybody know how
to read ALL packets that are send vie HTTP Post??

Any ideas?

Thx,

Sabine

Why don't you post what you've done, then we can suggest what might be wrong
....
 
S

shakah

You have to keep reading the response until you get the amount of bytes
specified in the response's Content-length header. Here's a code
excerpt that does a POST and reads a response:

java.net.URL url = new java.net.URL(sURL) ;
java.net.URLConnection urlc = url.openConnection() ;
urlc.setDoInput(true) ;
urlc.setDoOutput(true) ;
urlc.setUseCaches(false) ;
urlc.setRequestProperty("Content-Type", "text/xml") ;
urlc.setRequestProperty("Content-Length", ""+sPostData.length()) ;
urlc.setRequestProperty("Connection", "close") ;

// ...write the POST data
java.io_OutputStream os = urlc.getOutputStream() ;
os.write(sPostData.getBytes()) ;
os.flush() ;

// ...read the response
java.io.InputStream is = urlc.getInputStream() ;
int nContentLength = urlc.getContentLength() ;
byte [] ab = new byte[nContentLength] ;
int nRead=0 ;
while(nRead < nContentLength) {
nRead += is.read(ab, nRead, nContentLength - nRead) ;
}
sbResponse.append(new String(ab, "utf-8")) ;
os.close() ;
os = null ;
is.close() ;
is = null ;
((java.net.HttpURLConnection) urlc).disconnect() ;
urlc = null ;
 
T

Terrie

Hi shakah,

thanks for the response but my program still doesn't get the whole
content. I'll post my code:

HttpURLConnection httpcon =
(HttpURLConnection)url.openConnection();
String requestText = request.getRequest();
httpcon.setDoOutput(true);
httpcon.setDoInput(true);
httpcon.setUseCaches(false) ;
httpcon.setRequestProperty("Content-Type", "text/xml") ;
httpcon.setRequestProperty("Content-Length",
""+requestText.length()) ;
httpcon.setRequestProperty("Connection", "close") ;
httpcon.setRequestMethod("POST"); // HEAD
OutputStream os = httpcon.getOutputStream();
PrintWriter bw = new PrintWriter(httpcon.getOutputStream(),true);
bw.write(requestText);
bw.flush();

if(httpcon.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println(httpcon.getResponseMessage());
}
else {
InputStream is = httpcon.getInputStream();
int len = httpcon.getContentLength();
new EvalData(is, len);
// here I print out the is
}

Thx
 
J

JScoobyCed

Terrie said:
Hi shakah,

thanks for the response but my program still doesn't get the whole
content. I'll post my code:

HttpURLConnection httpcon =
(HttpURLConnection)url.openConnection();
String requestText = request.getRequest();
httpcon.setDoOutput(true);
httpcon.setDoInput(true);
httpcon.setUseCaches(false) ;
httpcon.setRequestProperty("Content-Type", "text/xml") ;
httpcon.setRequestProperty("Content-Length",
""+requestText.length()) ;
httpcon.setRequestProperty("Connection", "close") ;
httpcon.setRequestMethod("POST"); // HEAD
OutputStream os = httpcon.getOutputStream();
PrintWriter bw = new PrintWriter(httpcon.getOutputStream(),true);
bw.write(requestText);
bw.flush();

if(httpcon.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println(httpcon.getResponseMessage());
}
else {
InputStream is = httpcon.getInputStream();
int len = httpcon.getContentLength();
new EvalData(is, len);
// here I print out the is
}

Thx


Did u try to count how many CR+LF is there in your response ? I remember
such problem while reading an http response: the Content-Lenght was ,
say, 1000. I read 1000 ans I was missing a part of the content, let's
say 100. Then I read the InputStream without limit (and stopped the
program by forced shutdown), and counted the number of CR+LF: it was 100 !
Then I changed my code to detect CR+LF and only increment by one for the
set of two bytes, instead of one per byte. And I got it working.
Now that was several years ago, and the servers where not the same
(Apache 1.3.1x ... not sure, under Solaris, and a windows 98 plateform
to run the Java program).

JScoobyCed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top