我面临着版本3.1.2的 okhttp3 库的问题,因为Azure调用了以下代码
//create proxy authenticator object
Authenticator proxyAuthenticator = new Authenticator() {
@Override
public okhttp3.Request authenticate(okhttp3.Route route, okhttp3.Response response) throws IOException {
String credential = Credentials.basic("user", "password");
return response.request().newBuilder().header("Proxy-Authorization", credential).build();
}
};
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
clientBuilder.connectTimeout(300, TimeUnit.SECONDS);
clientBuilder.writeTimeout(300, TimeUnit.SECONDS);
clientBuilder.readTimeout(300, TimeUnit.SECONDS);
clientBuilder.proxy(new Proxy(Proxy.Type.valueOf("HTTP"), new InetSocketAddress(proxyHost, proxyPort)));
clientBuilder.proxyAuthenticator(proxyAuthenticator);
Retrofit.Builder retroBuilder = new Retrofit.Builder();
retroBuilder.client(clientBuilder.build());
ResourceManagementClient armClient = new ResourceManagementClientImpl(baseURL, appTokenCreds,
clientBuilder, retroBuilder);
armClient.setLongRunningOperationRetryTimeout(100000);
armClient.setSubscriptionId(System.getenv(subscriptionId));
System.out.println("API version :" + armClient.getApiVersion());
try {
armClient.setSubscriptionId(subscriptionId);
ServiceResponse<List<Provider>> result = armClient.getProvidersOperations().list(null);
} catch (AzureResourceManagerException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我得到的错误:
Exception
HTTP
API version :2015-11-01
hi
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
)
Caused by: java.io.IOException: unexpected end of stream on null
at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:199)
at okhttp3.internal.io.RealConnection.createTunnel(RealConnection.java:251)
at okhttp3.internal.io.RealConnection.connectTls(RealConnection.java:175)
at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:148)
at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
at okhttp3.RealCall.getResponse(RealCall.java:240)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
at com.microsoft.rest.UserAgentInterceptor.intercept(UserAgentInterceptor.java:58)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
at com.microsoft.rest.retry.RetryHandler.intercept(RetryHandler.java:69)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
at com.microsoft.rest.credentials.TokenCredentialsInterceptor.sendRequestWithAuthorization(TokenCredentialsInterceptor.java:49)
at com.microsoft.rest.credentials.TokenCredentialsInterceptor.intercept(TokenCredentialsInterceptor.java:37)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
at com.microsoft.azure.CustomHeaderInterceptor.intercept(CustomHeaderInterceptor.java:133)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
at okhttp3.RealCall.execute(RealCall.java:57)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:177)
at com.microsoft.azure.management.resources.ProvidersOperationsImpl.list(ProvidersOperationsImpl.java:240)
at com.bmc.cloud.provider.azureconnect.arm.TestARM.getLocations(TestARM.java:201)
... 1 more
Caused by: java.io.EOFException: \n not found: size=0 content=...
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)
at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
... 26 more
答案 0 :(得分:0)
根据异常“无法加载类”org.slf4j.impl.StaticLoggerBinder“,似乎在项目的类路径中缺少一些用于记录器实现的jar库文件,例如log4j
&amp; slf4j-log4j
。请尝试将这些jar文件添加到项目中,并为1.2
&amp; 2.2
注明不同的版本log4j
或slf4j-log4j
。
与此同时,我在您的代码中发现了一些可能的问题,请参阅下文。
ResourceManagementClientImpl(String baseUrl, ServiceClientCredentials credentials, OkHttpClient.Builder clientBuilder, Retrofit.Builder retrofitBuilder)
,您似乎使用的是Azure SDK for Java的版本1.0.0-beta1
,因为构造函数仅适用于此版本。所以我建议使用像0.9
这样的稳定版本比beta版更好,请参阅http://mvnrepository.com/artifact/com.microsoft.azure/azure-mgmt-resources。
1.0.0-beta1
的示例VMCreateGuide.java
,我发现代码new Authenticator()
和Credentials.basic("user", "password")
不适合调用ARM API,或者它似乎设置为http客户端通过代理服务器使用凭据来调用Azure API。因此,如果您想通过porxy服务器使用资源管理器调用Azure,则代码似乎缺少ARM身份验证。如果没有,我认为你需要使用正确的班级ApplicationTokenCredentials
&amp;程序包com.microsoft.azure.credentials
中的UserTokenCredentials
进行身份验证,就像在示例VMCreateGuide.java
中进行身份验证一样。