Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache HttpClient进行SharePoint REST调用,首次获取后负载平衡失败_Java_Rest_Sharepoint_Sharepoint 2013_Apache Httpclient 4.x - Fatal编程技术网

Java 使用Apache HttpClient进行SharePoint REST调用,首次获取后负载平衡失败

Java 使用Apache HttpClient进行SharePoint REST调用,首次获取后负载平衡失败,java,rest,sharepoint,sharepoint-2013,apache-httpclient-4.x,Java,Rest,Sharepoint,Sharepoint 2013,Apache Httpclient 4.x,我偶然发现了SharePoint REST调用负载平衡器的问题。当我将其余的调用直接发送到Web服务器时,一切似乎都正常工作。但是,当REST调用被发送到负载平衡器时,只有第一个调用获得HTTP 200 OK状态,REST返回HTTP 401 Unauthorized。经过一些研究,我发现上面写着: 这不是一个错误。Windows网络堆栈中内置了一个安全修复程序,可防止解析为环回地址的计算机名接受连接,除非该计算机名与NETBIOS名称匹配 那篇文章中提到的唯一解决办法是更改服务器的配置,这不是

我偶然发现了SharePoint REST调用负载平衡器的问题。当我将其余的调用直接发送到Web服务器时,一切似乎都正常工作。但是,当REST调用被发送到负载平衡器时,只有第一个调用获得HTTP 200 OK状态,REST返回HTTP 401 Unauthorized。经过一些研究,我发现上面写着:

这不是一个错误。Windows网络堆栈中内置了一个安全修复程序,可防止解析为环回地址的计算机名接受连接,除非该计算机名与NETBIOS名称匹配

那篇文章中提到的唯一解决办法是更改服务器的配置,这不是我们部门愿意做的事情

目标是我们可以从Java应用程序执行SharePoint REST调用

例如,我制作了一个小测试程序,当我按下按钮时,它会向我的SharePoint REST API发出GET请求

当我调整Windows主机文件以将负载平衡url映射到某个Web服务器(在负载平衡器下)的ip地址时,一切似乎都正常工作:

//first button click
Executing request GET https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1') HTTP/1.1
[principal: domain.com\<username>] [workstation: <LBSERVER>]
----------------------------------------
HTTP/1.1 200 OK

//second button click
Executing request GET https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1') HTTP/1.1
[principal: domain.com\<username>] [workstation: <LBSERVER>]
----------------------------------------
HTTP/1.1 200 OK

经过进一步研究,我发现问题是由错误的cookie处理引起的。原因是Apache HTTP客户端使用了
CookieSpecs.DEFAULT
(RFC2109)。在将其更改为CookieSpecs.STANDARD(RFC6265)之后,它起到了作用

RequestConfig localConfig = RequestConfig.custom()
            .setCookieSpec(CookieSpecs.STANDARD)
            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
            .build();

httpClient = HttpClients.custom()
            .setDefaultCookieStore(new BasicCookieStore())
            .setDefaultCredentialsProvider(credsProvider)
            .setDefaultRequestConfig(localConfig)
            .build();
CloseableHttpClient httpClient;
CredentialsProvider credsProvider;
HttpClientContext context;
HttpHost targetHost;

Account account;

private void handleButtonAction(ActionEvent event) throws IOException {
    get("https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1')");
}

private void get(String uri) throws IOException {
    HttpGet httpget = new HttpGet(uri);

    System.out.println("\nExecuting request " + httpget.getRequestLine());

    CloseableHttpResponse response = httpClient.execute(targetHost, httpget, context);
    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());

    response.close();
}

private void openClient() throws IOException {
    System.out.println("Open client");

    httpClient = HttpClients.createDefault();
    targetHost = new HttpHost("lbserver.domain.com", 443, "https");
    context = HttpClientContext.create();

    credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(targetHost, AuthScope.ANY_REALM, "ntlm"), 
            new NTCredentials(account.getUsername(), account.getPassword(), "lbserver", "domain.com"));

    context.setCredentialsProvider(credsProvider);

    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}

private void closeClient() throws IOException {
    System.out.println("Close client");
    if (httpClient != null) {
        httpClient.close();
    }
}

public void initialize(URL url, ResourceBundle rb) {
    account = new Account();

    try {
        openClient();
    } catch (IOException ex) {
        Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
    }

    Runtime.getRuntime().addShutdownHook(new Thread(new Task() {
        @Override
        protected Object call() throws Exception {
            closeClient();
            return null;
        }
    }));
}
RequestConfig localConfig = RequestConfig.custom()
            .setCookieSpec(CookieSpecs.STANDARD)
            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
            .build();

httpClient = HttpClients.custom()
            .setDefaultCookieStore(new BasicCookieStore())
            .setDefaultCredentialsProvider(credsProvider)
            .setDefaultRequestConfig(localConfig)
            .build();