要添加什么依赖项以使hhtpclient示例工作

时间:2015-09-10 12:24:09

标签: java xml apache maven post

我正在处理我公司的软件项目,我需要测试一个http连接并将一个示例xml发送到服务器并获取响应。客户端向我们发送一个示例连接方法,我尝试测试它。下面是示例连接方法。

   public String postApplicationRequest(String content) throws Exception {
    final HttpPost post = new HttpPost("url");
    final HttpEntity entity = new StringEntity(content, ContentType.create("text/xml", "UTF-8"));
    post.setEntity(entity);
    final HttpClient client = new DefaultHttpClient();
    AuthScope authScopre = new AuthScope("www.myhost.com", 80, AuthScope.ANY_REALM);
    Credentials credentials = new UsernamePasswordCredentials("username", "password");
    HttpState httpState = client.getState();
    httpState.setCredentials(authScopre, credentials);
    final HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() != 200) {
        System.out.println("call was not successful");
        return null;
    }
    return IOUtils.toString(response.getEntity().getContent());
}

我将依赖项添加到pom.xml

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>

但是没有找到HttpEntity导入。实际上我很困惑我需要添加哪个版本的httpClient pom.xml。还有任何其他依赖项,我需要运行此代码示例。

任何建议都会非常有帮助。谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

尝试

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpcore</artifactId>
  <version>4.4.2</version>
</dependency>