Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 如何使用Jersey(JAX-RS)向NTLM认证服务器发出简单的POST请求_Java_Rest_Jersey_Jax Rs_Ntlm - Fatal编程技术网

Java 如何使用Jersey(JAX-RS)向NTLM认证服务器发出简单的POST请求

Java 如何使用Jersey(JAX-RS)向NTLM认证服务器发出简单的POST请求,java,rest,jersey,jax-rs,ntlm,Java,Rest,Jersey,Jax Rs,Ntlm,我在网上查找了以下文章: 以及: 第二个问题的答案只提供了GET请求的解决方案。我正在按照以下方式设置我的客户: private void init() { if (client == null) { client = ClientBuilder.newClient(prepareClientConfig()); } } private ClientConfig prepareClientConfig() { ClientConfig config

我在网上查找了以下文章:

以及:

第二个问题的答案只提供了GET请求的解决方案。我正在按照以下方式设置我的客户:

private void init() {
    if (client == null) {
        client = ClientBuilder.newClient(prepareClientConfig());
    }
}

private ClientConfig prepareClientConfig() {
    ClientConfig config = new ClientConfig();
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new NTCredentials(userId, password, null, null));
    config.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    config.connectorProvider(new ApacheConnectorProvider());
    return config;
}
下面是我提出GET请求的地方:

@Override
public Response get(String uri, List<HttpUrlParameter> params) throws WebCallException {
    init();
    String url = baseUrl + restUri + apiVersion + uri;
    WebTarget webTarget = client.target(url);
    for (HttpUrlParameter param : params) {
        webTarget = webTarget.queryParam(param.getKey(), param.getValue());
    }
    Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
    return response;
}
我能够通过Postman进行POST调用,没有任何问题,因此我知道调用(在我的Java代码之外)工作正常。当我使用Jersey进行POST通话时,我得到以下例外情况:

javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException
        at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:481)
        at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
        at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
        at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
        at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
        at com.harpercollinschristian.integration.util.rest.crm.RestApiClientCrmImpl.post(RestApiClientCrmImpl.java:119)
        at com.harpercollinschristian.integration.service.crm.CrmRestfulApi$4.request(CrmRestfulApi.java:127)
        at com.harpercollinschristian.integration.util.rest.crm.CrmRestApiRequest.exec(CrmRestApiRequest.java:53)
        at com.harpercollinschristian.integration.service.crm.CrmRestfulApi.createAccount(CrmRestfulApi.java:132)
        at com.harpercollinschristian.integration.service.crm.CrmRestfulApiTest.testCreateAccount(CrmRestfulApiTest.java:80)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.http.client.ClientProtocolException
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
        at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:435)
        ... 44 more
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        ... 46 more
在我看来,传递给post()方法的JSON实体可能被设置为只能调用一次的流?我需要为所有POST和补丁调用发送的JSON消息并不长,可以使用FasterXML/Jackson库轻松地表示为字符串,因此我们应该能够轻松地“重复”实体。我知道我可能遗漏了一些非常明显的东西,但我找不到解决办法。该项目正在使用Jersey客户端对另外两个非Microsoft(因此也非NTLM)服务进行REST API调用,因此我需要使用Jersey来实现这些服务(换句话说,如果我能帮助的话,我不想退出Jersey)


任何帮助都将不胜感激

我从我们的Microsoft CRM合作伙伴那里收到了一段代码片段,它给出了答案。设置需要更新的配置的方法:

private ClientConfig prepareClientConfig() {
    ClientConfig config = new ClientConfig();
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    final AuthScope ntlmAuthScope = new AuthScope(null, -1, AuthScope.ANY_REALM, "NTLM");
    credentialsProvider.setCredentials(ntlmAuthScope, new NTCredentials(userId, password, null, null));
    config.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
    config.connectorProvider(new ApacheConnectorProvider());
    return config;
}
注意请求\实体\处理的config属性-设置为BUFFERED。我相信,这正是我们所需要的,以允许对实体进行缓冲,从而为后续请求重用实体(以满足NTLM协议)。还有一些其他的补充(例如,特定的AuthScope)

javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException
        at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:481)
        at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
        at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
        at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
        at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
        at com.harpercollinschristian.integration.util.rest.crm.RestApiClientCrmImpl.post(RestApiClientCrmImpl.java:119)
        at com.harpercollinschristian.integration.service.crm.CrmRestfulApi$4.request(CrmRestfulApi.java:127)
        at com.harpercollinschristian.integration.util.rest.crm.CrmRestApiRequest.exec(CrmRestApiRequest.java:53)
        at com.harpercollinschristian.integration.service.crm.CrmRestfulApi.createAccount(CrmRestfulApi.java:132)
        at com.harpercollinschristian.integration.service.crm.CrmRestfulApiTest.testCreateAccount(CrmRestfulApiTest.java:80)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.http.client.ClientProtocolException
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
        at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:435)
        ... 44 more
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        ... 46 more
private ClientConfig prepareClientConfig() {
    ClientConfig config = new ClientConfig();
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    final AuthScope ntlmAuthScope = new AuthScope(null, -1, AuthScope.ANY_REALM, "NTLM");
    credentialsProvider.setCredentials(ntlmAuthScope, new NTCredentials(userId, password, null, null));
    config.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
    config.connectorProvider(new ApacheConnectorProvider());
    return config;
}