为什么org.apache.httpcomponents在Java中工作?

时间:2017-06-08 18:17:06

标签: java maven intellij-idea

所以,问题是:我无法加载导入。帮助...

我从网上获取此代码只是为了试验它是如何工作的。问题是a"类,接口或枚举所期望的错误。"由于顶部的依赖性,它只是不会编译并不断要求JAR。在看到这个程序之前,我甚至都不知道依赖是什么。有人可以告诉我它为什么不工作,并尝试解决它?我尝试了所有.JAR Intellij建议,然后我尝试搜索问题,但大多数答案都是无关紧要或太复杂涉及Maven或其他东西。请帮忙......

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

import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class HttpDocumentExistsWithHttpClient {

/**
 * check if a document exists in a sharepoint library
 */
public static void main(String[] args) throws Exception{
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))
            .build();


    String user = "myusername";
    String pwd = "mypassword";
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials(user, pwd, "", ""));

    // You may get 401 if you go through a load-balancer.
    // To fix this, go directly to one of the sharepoint web server or
    // change the config. See this article :
    // http://blog.crsw.com/2008/10/14/unauthorized-401-1-exception-calling-web-services-in-sharepoint/
    HttpHost target = new HttpHost("web01.mysharepoint.local", 80, "http");
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);

    // The authentication is NTLM.
    // To trigger it, we send a minimal http request
    HttpHead request1 = new HttpHead("/");
    CloseableHttpResponse response1 = null;
    try {
        response1 = httpclient.execute(target, request1, context);
        EntityUtils.consume(response1.getEntity());
        System.out.println("1 : " + response1.getStatusLine().getStatusCode());
    }
    finally {
        if (response1 != null ) response1.close();
    }

    // The real request, reuse authentication
    String file = "/30500C/PubDoc/TEST/jira.log";  // source
    HttpGet request2 = new HttpGet("/_api/web/GetFileByServerRelativeUrl('" + file + "')/Etag");
    CloseableHttpResponse response2 = null;
    try {
        response2 = httpclient.execute(target, request2, context);
        EntityUtils.consume(response2.getEntity());
        int rc = response2.getStatusLine().getStatusCode();
        String reason = response2.getStatusLine().getReasonPhrase();
        if (rc != HttpStatus.SC_OK) {
            System.out.println(file + " is missing.  Reason : "
                    + reason + "   rc : " + rc + "(500 is the equivalent of NOT FOUND)");
        }
        else {
            System.out.println(file + " exists.");
        }
    }
    finally {
        if (response2 != null) response2.close();
    }
    return;
}
}

我真的只想在JGrasp和IntelliJ中编译它。没有Java经验。

1 个答案:

答案 0 :(得分:0)

首先,Maven是一个依赖管理和构建工具,如果已经设置,它可以自动为您下载依赖项。

第一段代码(<dependency>...)是maven获取依赖项的指令。它属于您班级以外的pom.xml文件。

如果您没有为此设置并且不想进入此设置,则可以在https://hc.apache.org/downloads.cgi下载所需的JAR。并确保将其添加到类路径中。

我建议你熟悉像Maven这样的工具,因为在使用Java时你会遇到依赖关系,从长远来看它确实简化了很多事情。