• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet post to server using HttpUrlConnection

 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,


In my servlet I will send data to a URL using method 'POST' with HttpUrlConnection class.

But it happens to return me http code=411 from server which indicates content-length field.

I try to send few data like "userid=username&pass=1234&msg=try+again"

Here is my part of code:


My question is

1: Does content-length field is necessary in a POST method ?
2: Even if I coded "connection.setRequestProperty("Content-length", length) " after connection.setRequestMethod("POST"), It will give me NO return_code value. Why?


Can anyone please help me..i'm stuck for few days..


thanks in advance.

Nick
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Content-length is not generally necessary but since you got a 411, the service you are calling requires it.
2. NO return value means what? did that print statement even execute?

Where did you write the body of the POST?

Bill
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Brogden wrote:1. Content-length is not generally necessary but since you got a 411, the service you are calling requires it.
2. NO return value means what? did that print statement even execute?

Where did you write the body of the POST?

Bill





params string contents the body...


Is that the way of posting params to server using POST method?
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've try again this morning but this time i added in content-length...but it wont retun me the response http code




The result is :


b4 calling connect()
after calling connect()



"return_code" is not printed.
I dont understand, why it cannot return me the value of response code?

anyone help ?

Nick
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

On the first look, I would say that the API call getResponseCode() throws IOException.

Try to replace the existing call (Exception block):
- e.printStackTrace(); // standard error stream
with the following one:
- System.out.println("Exception : " + e.toString()); // standard output stream
and let us know the result.



Regards,
Rok
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think that URL constructor works the way you are trying to use it.

Surely that last parameter

creates a String which is NOT a file name and a MalformedURLException is thrown.

Bill
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think so. That would cause the "b4 calling connect()" and "after calling connect()" to not be printed, as they come after the construction of the URL.

Nicky, I just want to know: is the getResponseCode() method "hanging", or is an exception thrown and printed?

And Rok, the full stack trace is always more useful for debugging than just the toString() value which is just the class and message. The stack trace also indicates exactly where the exception is thrown from.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Check this COde ... ITs working for me :)

URL url = null;
String result = "";
HttpURLConnection connection = null;
try{
//Create connection
url = new URL(desiredUrl);

connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","text/html");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Length", Integer.toString("your message length here ".length()));
connection.setAllowUserInteraction(true);
connection.setRequestProperty("Protocol-Version",httpVersion);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);


if(connection.getResponseCode() == 200)
{
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(jsonText);
wr.flush();
wr.close();

// get data
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
// Print HERE
}
rd.close();

}

} catch (Exception e)
{

e.printStackTrace();
}finally{
if(connection != null) {
connection.disconnect();
}
}




 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic