Marven
<!--https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient-->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!--https://mvnrepository.com/artifact/commons-logging/commons-logging-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--https://mvnrepository.com/artifact/avalon-framework/avalon-framework-->
<dependency>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework</artifactId>
<version>4.1.5</version>
</dependency>
<!--Https://mvnrepository.com/artifact/logkit/logkit-->
<dependency>
<groupId>logkit</groupId>
<artifactId>logkit</artifactId>
<version>2.0</version>
</dependency>
<!--Https://mvnrepository.com/artifact/commons-codec/commons-codec-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<!--represents the introduction of development when the package is not loaded when it is released-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
/**
*
* @param URL
* @param user
* @param password
* @param jsonparams
* @return
*/
public static string postbycredential (string url, string user, string password, string jsonparams) {
String ret = "";
HttpClient httpclient = null;
Postmethod method = null;
Usernamepasswordcredentials creds = null;
Stringrequestentity requestentity = null;
try {
HttpClient =new httpclient (New Httpclientparams (), New Simplehttpconnectionmanager (true);
creds = new Usernamepasswordcredentials (user, password);
Httpclient.getstate (). SetCredentials (Authscope.any, creds);
method = new Postmethod (URL);
Method.setdoauthentication (TRUE);
requestentity = new Stringrequestentity (jsonparams, "Application/json", "UTF-8");
Method.setrequestentity (requestentity);
Httpclient.executemethod (method);
Ret= method.getresponsebodyasstring ();
SYSTEM.OUT.PRINTLN (ret);
catch (HttpException e) {
E.printstacktrace ();
catch (Unsupportedencodingexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (method!= null) {
Method.releaseconnection ();
}
return ret;
}
}
There's an article about connection closure.
http://seanhe.iteye.com/blog/234759