HttpClient GET错误 - java.lang.ClassCastException:[B不能强制转换为java.lang.String

时间:2017-08-16 20:28:49

标签: java apache web-services httpclient

当我尝试使用 HttpClient 进行DO时,我收到以下错误:

java.lang.ClassCastException: [B cannot be cast to java.lang.String
    at org.apache.http.conn.ssl.DefaultHostnameVerifier.getSubjectAltNames(DefaultHostnameVerifier.java:309)
    at org.apache.http.conn.ssl.DefaultHostnameVerifier.verify(DefaultHostnameVerifier.java:112)
    at org.apache.http.conn.ssl.DefaultHostnameVerifier.verify(DefaultHostnameVerifier.java:99)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:463)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:397)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at br.com.serasa.captacao.framework.web.http.HttpHelper.executeRequestForAnyResponse(HttpHelper.java:735)
    at br.com.serasa.captacao.framework.web.http.HttpHelper.executeHtmlRequest(HttpHelper.java:463)
    at br.com.serasa.captacao.framework.web.http.HttpHelper.executeHtmlRequest(HttpHelper.java:448)
    at br.com.serasa.captacao.framework.web.page.Page.requestHtml(Page.java:98)
    at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPage.abrirPaginaInicial(DareSpPage.java:37)
    at br.com.fiveware.dare.sp.emissao.guias.captador.CaptadorDareSP.abrirPaginaInicial(CaptadorDareSP.java:30)
    at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPageTest.abrirPaginaInicialTest(DareSpPageTest.java:29)
    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:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    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:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我的推荐人:https://portaldecustas.tjsp.jus.br/portaltjsp/login.jsp

我的目标: https://portaldecustas.tjsp.jus.br/portaltjsp/

我的GET配置: [expectContinueEnabled = false,proxy = null,localAddress = null,cookie_pec = null,redirectsEnabled = true,relativeRedirectsAllowed = true,maxRedirects = 50,circularRedirectsAllowed = false,authenticationEnabled = true,targetPreferredAuthSchemes = null,proxyPreferredAuthSchemes = null,connectionRequestTimeout = 1000,connectTimeout = 60000,socketTimeout = 60000,contentCompressionEnabled = true]

(我搜索过此错误,但我没有找到针对此特定情况的解决方案)

代码:

HttpGet get = newGetMethod(getTarget(), getTimeoutHandler());
HtmlNode htmlNode = requestHtml(get);

...当newGetMethod它是我公司框架的一种方法时:

/**
 * Cria um get
 * 
 * @param target Endere?o acessado
 * @param timeoutHandler Controla o timeout
 * @return Get
 * @throws HarvestException Quando ocorre um erro com o tempo a ser usado
 */
public static HttpGet newGetMethod(URI target, TimeoutHandler timeoutHandler) throws HarvestException {
    HttpGet method = newGetMethod(target, timeoutHandler.calculate());
    method.getParams().setParameter(TIMEOUT_HANDLER_PARAM, timeoutHandler);
    return method;
}

2 个答案:

答案 0 :(得分:0)

这是我的.pom文件中的Http客户端版本

我解决了这个问题:

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

答案 1 :(得分:0)

如果您使用的是httpclient库的版本4.5.35.0 Alpha1,则是由于以下错误-https://issues.apache.org/jira/browse/HTTPCLIENT-1836

更新为4.5.4 / 5.0 Alpha2或更高版本。