• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTTPS POST Request with parameters in Java

 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a URL which when triggered from Javascipt/AJAX am able to capture the response using xmlHTTPResponse.

But I would like to do the same in Java, pass the variables as input params and get the same response in Java using URL/ HTTPUrlConnection.

Please Help!

Thanks in Advance
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Some minimal googling found this



Thanks, I am already using HttpUrlConnector but when I add the property requestmethod as "POST", I get an error. Is HttpUrlConnector POST by default.?


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrithik khurana wrote: I get an error.

What is the error?
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Hrithik khurana wrote: I get an error.

What is the error?



java.io.IOException: Server returned HTTP response code: 411 for URL: ...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lookup what the error means. Here's a list of HTTP response codes. About code 411, it says:
10.4.12 411 Length Required

The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.

So, the Content-Length header is missing from the request. You must make sure that it is included.

It would be easier to help you if you would post your Java code.
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpURLConnection connection =null;
try {
//String encodedUrl = URLEncoder.encode(myURL + "?" + query,"UTF-8");
String encodedUrl = myURL + "?" + query;

connection = (HttpsURLConnection)new URL(encodedUrl).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-length",""+query.length());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// connection.setRequestMethod("POST");


BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
connection.disconnect();
}


I get the 411 error if I uncomment the line connection.setRequestMethod("POST");


Please help..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic