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");编译空格,却依旧报错
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。