• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using HttpUrlConnection

 
Alaa Abutabaq
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I'm facing problem in Using HttpUrlConnection. When I submit a request to sun java web server 1.6 . By using the following code I request the server , The server receives the request but can not read the request body that is written on the connection output stream.

my client code :

URL page = new URL(url);
HttpURLConnection sock = (HttpURLConnection) page.openConnection();
sock.setRequestMethod("POST");
sock.setRequestProperty("Content-Length",
"" + Integer.toString(body.getBytes().length));
sock.setUseCaches(false);
sock.setDoOutput(true);
if (body.length() > 0) {
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeBytes(body);
out.flush();
out.close();
}
sock.connect();
BufferedReader in = new BufferedReader (new InputStreamReader(sock.getInputStream(),"Cp1256"));
int nHttpResponseCode = sock.getResponseCode();
if(nHttpResponseCode != HttpURLConnection.HTTP_OK){
System.out.println("\tHttp error:" + nHttpResponseCode );
in.close();
return nHttpResponseCode;
}
//receive reply
.....
.....
in.close();
return nHttpResponseCode;

My server code is :

String params = null;
try {
java.io.InputStream in = request.getInputStream();
byte b[] = new byte[1000]; //in.available()];
in.read(b);
char[] s = new char[b.length];
for (int i = 0; i < b.length; i++) {
if (b == 0)break;
s= (char) b;
}
params = new String(s);

}
catch (Exception exp) {
exp.printStackTrace();
}


I tried to submit the request after appending the request body to the url (url like that http://ipadd/webapp/page.jsp?appndedVlaues) and retreived the appended string by calling getQueryString method and It worked well.


Does anybody faced such a problem before?
please help

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not expert enough on this to spot what's wrong with that, but here's something of mine that runs fine. Maybe you can spot something in the difference.

[ March 01, 2006: Message edited by: Stan James ]
 
Alaa Abutabaq
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan. As a matter of fact I discovered new fact about it. What is happening is that I include another code that do something with the request(call getParameter() method) before reading data from input stream. I really don't know exactly what is the mechanism that prevent input stream from reading or what is the server behaiour, but removing that part of code or calling it after retrieving data from input stream makes every thing goes well. If you know how the server deals with this situation, that will be great .

Thanks in advance
 
Frank Lee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Maybe it's a bit late, but I faced the same problem as you did and I solved my problem by NOT using inputstream.available() immediately.

This is because of the relatively slow response time of a http connection.

After the httpConnection is setup, inputStream.available() may or may not return true, depending on whether data from the client has been received at the server's end.

How do I overcome this? I use a inputStream.read() to start a blocking call to wait until a character has arrived in the inputStream, and subsequently use inputSteam.available() to get the rest of the request from the inputStream.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch, Frank!

Yes, this is months old ... available() only tells you what's available right now, not what might become available in a little while to a blocking read(). Leave available out, just read until there ain't no mo: a null readline or a -1 byte count.
[ October 11, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!