wood burning stoves 2.0*
The moose likes Java in General and the fly likes HTTPS POST Request with parameters in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HTTPS POST Request with parameters in Java" Watch "HTTPS POST Request with parameters in Java" New topic
Author

HTTPS POST Request with parameters in Java

Kishen Singh Punjabi
Ranch Hand

Joined: Sep 14, 2012
Posts: 65
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

Joined: May 22, 2012
Posts: 867
    
    5
Some minimal googling found this
Kishen Singh Punjabi
Ranch Hand

Joined: Sep 14, 2012
Posts: 65
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

Joined: Aug 16, 2005
Posts: 14116
    
  16

Hrithik khurana wrote: I get an error.

What is the error?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kishen Singh Punjabi
Ranch Hand

Joined: Sep 14, 2012
Posts: 65
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

Joined: Aug 16, 2005
Posts: 14116
    
  16

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

Joined: Sep 14, 2012
Posts: 65
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTTPS POST Request with parameters in Java