当前位置: 首页 > 知识库问答 >
问题:

Java URIBuilder位于加载程序“app”的未命名模块中

梁丘逸仙
2023-03-14

我想执行GET请求:

    HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
    URIBuilder uriBuilder = new URIBuilder(request.getURI())
            .addParameter("code", "001")
            .addParameter("name", "AAA")
           

    String auth = user + ":" + mdp;

    byte[] encodedAuth = Base64.encodeBase64(
            auth.getBytes(StandardCharsets.ISO_8859_1));

    String authHeader = "Basic " + new String(encodedAuth);

    request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute((HttpUriRequest) uriBuilder);

    int statusCode = response.getStatusLine().getStatusCode();

当我试图捕获我的HTTPResponse客户端时。执行((HttpUriRequest)uriBuilder) 我有以下错误:

java.lang.ClassCastException: class org.apache.http.client.utils.URIBuilder cannot be cast to class org.apache.http.client.methods.HttpUriRequest (org.apache.http.client.utils.URIBuilder and org.apache.http.client.methods.HttpUriRequest are in unnamed module of loader 'app')

共有1个答案

冀景明
2023-03-14

您正面临此错误,因为您正试图将URIBuilder的实例强制转换为HttpUriRequest

您需要创建一个适当的HttpUriRequest实现来执行HTTP请求。

在你的用例中,我想它应该是这样的:

    URIBuilder uriBuilder = new URIBuilder(URL_SECURED_BY_BASIC_AUTHENTICATION)
            .addParameter("code", "001")
            .addParameter("name", "AAA");

    URI uri = uriBuilder.build();  
           
    HttpGet request = new HttpGet(uri);

    String auth = user + ":" + mdp;

    byte[] encodedAuth = Base64.encodeBase64(
            auth.getBytes(StandardCharsets.ISO_8859_1));

    String authHeader = "Basic " + new String(encodedAuth);

    request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(request);

    int statusCode = response.getStatusLine().getStatusCode();

在您的评论中,您似乎正试图使用SSL连接到一个网站:问题是您的Java代码不信任服务器。

您需要配置一个有效的证书链,并指示您的代码使用它来解决这个问题。

Apache Client不依赖于标准的JSSE机制来实现这一目的。相反,您需要使用适当的TrustManager配置SSLContext。请参阅以下代码(第一部分来自Android开发者站点中的这个非凡的留档片段):

// Load your server certificate
CertificateFactory cf = CertificateFactory.getInstance("X.509");

InputStream input = new BufferedInputStream(
  new FileInputStream("server.crt")
);

Certificate certificate;
try {
  certificate = cf.generateCertificate(input);
} finally {
  input.close();
}

// Create an in-memory KeyStore containing the server certificate
// It is required in order to configure the TrustManager
String keyStoreType = KeyStore.getDefaultType(); // JKS
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("cert", certificate);

// Create a TrustManager that trusts the server certificates in the KeyStore
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
  TrustManagerFactory.getDefaultAlgorithm()
);
trustManagerFactory.init(keyStore);

// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trustManagerFactory.getTrustManagers(), null);

// Now, the actual Apache Client part

//Create a SSLConnectionSocketFactory and pass it the above created SSLContext
SSLConnectionSocketFactory factory = 
  new SSLConnectionSocketFactory(sslcontext, new NoopHostnameVerifier()
);

//Create the actual HttpClient
CloseableHttpClient client = HttpClients
  .custom()
  .setSSLSocketFactory(factory)
  .build()
;

// Use this client to perform your HTTP invocation

HttpResponse response = client.execute(request);

int statusCode = response.getStatusLine().getStatusCode();

您可以从浏览器或使用类似于openssl的工具获取服务器证书。请看这个伟大的问题。

 类似资料: