开发者社区> 问答> 正文

tomcat请求阿里云发票识别问题

String host = "https://ocrapi-document.taobao.com"; String path = "/ocrservice/document"; String method = "POST";

String appcode = "83359fd73fe94948385f570e3c139105";//自己去阿里云申请生成 Map<String, String> headers = new HashMap<String, String>(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); //根据API的要求,定义相对应的Content-Type headers.put("Content-Type", "application/json; charset=UTF-8"); Map<String, String> querys = new HashMap<String, String>(); String bodys = "{"img":""+ imgToBase64(pathName)+""}"; String fileBody=""; try { /** * 重要提示如下: * HttpUtils请从 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java * 下载 * * 相应的依赖请参照 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml */ HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); System.out.println(response.toString()); //获取response的body System.out.println(EntityUtils.toString(response.getEntity()));

        fileBody = EntityUtils.toString(response.getEntity());
  
  
  
  以上代码在JVM测试时正常运行,在web项目中,部署在tomcat7时,返回
  HTTP/1.1 400 Bad Request [Date: Wed, 17 Nov 2021 01:48:02 GMT, Content-Type: application/oct-stream, Content-Length: 0, Connection: keep-alive, Keep-Alive: timeout=25, X-Ca-Error-Message: Invalid AppCode `not exists`, Server: Kaede/3.5.3.583 (sz00i3n93), X-Ca-Error-Code: A400AC

A400AC 400 Invalid AppCode ${Reason} 当使用AppCode模式授权时,未找到AppCode 怀疑可能是code前面存在空格,已经用URLEncoder.encode(" ", "UTF-8");编译空格,却依旧报错

展开
收起
游客s4zpwa4kzwmhg 2021-11-17 14:39:29 788 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Apache Tomcat 的云原生演进 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载