需要导入:commons-httpclient-3.1.jar
public static final Integer CONNECTION_TIMEOUT = 180000;
private String ngenUrlValue = Config.get("http.url.value");
private String loginRealmValue = Config.get("http.login.realm.value");
private String loginPrincipalValue = Config.get("http.login.principal.value");
private String loginCredentialsValue =Config.get("http.login.credentials.value");
private HttpClient client;
private PostMethod post;
public NgenMessageService ngenMessageService;
HttpClientParams params = new HttpClientParams();
params.setSoTimeout(CONNECTION_TIMEOUT);
client = new HttpClient(params);
URL url = new URL(ngenUrlValue);
String host = url.getHost();
int port = url.getPort();
if (port == -1) {
port = 80;
}
AuthScope aus;
if (StringUtils.isNotBlank(loginRealmValue))
aus = new AuthScope(host, port, AuthScope.ANY_REALM);
else {
aus = new AuthScope(host, port, loginRealmValue);
}
if (StringUtils.isNotBlank(loginPrincipalValue) && StringUtils.isNotBlank(loginCredentialsValue)) {
UsernamePasswordCredentials upc = new UsernamePasswordCredentials(loginPrincipalValue, loginCredentialsValue);
client.getState().setCredentials(aus, upc);
}
post = new PostMethod(ngenUrlValue);
StringRequestEntity entity = new StringRequestEntity(“content”, null, Charset.forName("UTF-8").name());
post.setRequestEntity(entity);
client.executeMethod(post);
String responseXmlStr = post.getResponseBodyAsString();//得到返回的结果