When you want to post some values to a specific URL using java you can use http components fromĀ http://hc.apache.org/httpclient-3.x/

 

You can use part of the following code

DefaultHttpClient httpclient = new DefaultHttpClient();
// The following is if you want to pass http authentication credentials
httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("posturl", 80),
                new UsernamePasswordCredentials("username", "password"));
// Now pass some parameters to the url
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", value1));
params.add(new BasicNameValuePair("param2", value2));
String charset="your charset";
try{
   UrlEncodedFormEntity query = new UrlEncodedFormEntity(params, charset);
   HttpPost post = new HttpPost(posturl);
   post.getParams().setParameter("http.protocol.content-charset", charset);
   post.getParams().setParameter("http.useragent", "Test Client");
   post.setEntity(query);
   ResponseHandler<String> responseHandler = new BasicResponseHandler();
   String responseBody = httpclient.execute(post, responseHandler);
   }
catch (Exception e) {
  logger.error(e);
} finally {
  // close the connection
  httpclient.getConnectionManager().shutdown();     
   }

 

By admin