Java HTTP 客户端内容类型错误



即使我为 text/html 设置了内容类型(如错误所示(,当我尝试执行GET请求时,我也会收到内容类型错误:

br.com.fiveware.daresp.emissao.guias.captador.exception.NavegacaoException: Erro ao tentar validar CPF/CNPJ do contribuinte
at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPage.validarCpfCnpj(DareSpPage.java:137)
at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPage.informarDadosContribuinte(DareSpPage.java:85)
at br.com.fiveware.dare.sp.emissao.guias.captador.CaptadorDareSP.informarDadosContribuinte(CaptadorDareSP.java:66)
at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPageTest.informarDadosContribuinte(DareSpPageTest.java:98)
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)
Caused by: br.com.serasa.captacao.framework.web.http.UnexpectedContentTypeHarvestException: Unexpected content type [application/json;charset=UTF-8] received, was expecting one of [text/html, text/plain, application/xhtml+xml, application/xml, text/xml, application/x-javascript, text/javascript] on method [GET https://portaldecustas.tjsp.jus.br/portaltjsp/pages/cpfcnpj/xxxxxxxxxxx/validar.json?_=1503494530352]
at br.com.serasa.captacao.framework.web.http.HttpHelper.validateExpectedContentType(HttpHelper.java:1004)
at br.com.serasa.captacao.framework.web.http.HttpHelper.validateRequiredContentType(HttpHelper.java:957)
at br.com.serasa.captacao.framework.web.http.HttpHelper.executeHtmlRequest(HttpHelper.java:466)
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.validarCpfCnpj(DareSpPage.java:132)
... 26 more

这是我的代码:

private void validarCpfCnpj(DadosContribuinte contribuinte) throws HarvestException
{
LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
String url = "https://portaldecustas.tjsp.jus.br/portaltjsp/pages/cpfcnpj/" + contribuinte.getCnpj()
+ "/validar.json?_=1503494530352";
HttpGet get = newGetMethod(URI.create(url), getTimeoutHandler());
get.addHeader("Accept", "text/plain, */*; q=0.01");
get.addHeader("Content-type", "text/plain");
try {
setHtml(requestHtml(get));
validarPaginaAtualPorTexto(getHtml(), contribuinte.getNome());      
} catch (HarvestException e) {
log.error("Erro ao tentar informar dados do contribuinte (formulário)n" + e.getMessage());
throw new NavegacaoException("Erro ao tentar validar CPF/CNPJ do contribuinte", e);
}
}

这些是浏览器上的 URL 参数:

_=1503495545749

的,键为空,生成的值是随机的。

这是请求标头:

GET /portaltjsp/pages/cpfcnpj/43524823823/validar.json?_=1503495545749 HTTP/1.1
Host: portaldecustas.tjsp.jus.br
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0
Accept: text/plain, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://portaldecustas.tjsp.jus.br/portaltjsp/pages/custas/new
X-Requested-With: XMLHttpRequest
Cookie: JSESSIONID=CACF3C9BD9F8633A42C2525DF994B247; siscondj=%7B%22contraste%22%3A0%2C%22fontes%22%3A100%2C%22escalabilidade%22%3A0%7D
Connection: keep-alive

您的URL返回"application/json"内容类型,请尝试在"Accept"标头中使用它:

HttpGet get = newGetMethod(URI.create(url), getTimeoutHandler());
get.addHeader("Accept", "application/json");
get.addHeader("Content-type", "text/plain");

相关内容

  • 没有找到相关文章