文件长度:165924
文件大小:162KB
URL:https://recognition.image.myqcloud.com/ocr/drivinglicence
Authorization:iWv1dxpmmidbiRE0u2ZZHoLbLv9hPTEyNTk2MTIxODAmYj10ZXN0Jms9QUtJRG5Pa3FnSjB4eTFyVHB3ZFV3OUxLNFlucWZGYzBjQXNRJnQ9MTU2MjczMDYwNCZlPTE1NjQzMTQ5MzQ5MzQmcj0xMjU1NzMzOTkw
返回参数:{"code":-1308,"message":"DOWNLOAD_FAILED_ERROR","seq":"53b6e281-e4f2-425d-b1f8-78c29be57569","data":{}}
请求代码如下:
/**
* 1、每个请求的包体大小限制为6MB
*/
public static void main(String[] args) {
// 1、读取图片的Base64
String fileName = "C:\\Disk-F\\driver-front.jpg";
String base64pic = getStreamString(fileName);
// 2、签名
long expireTime = System.currentTimeMillis() + 6 * 60 * 60 * 1000; // 6小时
String sign = null;
try {
sign = getSign("test", expireTime);
} catch (Exception e) {
e.printStackTrace();
}
// 3、生成请求JSON数据
JSONObject json = new JSONObject();
json.put("appid", APP_ID);
json.put("bucket", "test");
json.put("type", "0"); // 0 表示行驶证,1 表示驾驶证,2 表示行驶证副页
json.put("url", base64pic);
// 4、发送请求
sendPayRequest(DRIVER_URL, sign, json.toJSONString());
}
// Content-Length 要判断一下 strReqJsonStr 的大小 不能超过 6M = 6*1024*1024
public static String sendPayRequest(String apiUrl, String sign, String strReqJsonStr) {
String resultStr = null;
HttpURLConnection httpURLConnection = null;
PrintWriter out = null;
BufferedReader in = null;
try {
URL url = new URL(apiUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Authorization", sign); // 设置签名
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Content-Length", "165924");
httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
// 发送POST请求参数
out = new PrintWriter(httpURLConnection.getOutputStream());
out.write(strReqJsonStr);
out.flush();
// 读取响应
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
StringBuffer content = new StringBuffer();
String tempStr = null;
in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((tempStr = in.readLine()) != null) {
content.append(tempStr);
}
System.out.println("================== 请求后收到返回参数:" + content.toString());
// 转换成json对象
com.alibaba.fastjson.JSONObject respJson = JSON.parseObject(content.toString());
return respJson.toString();
} else {
System.out.println("================== 网络异常:" + httpURLConnection.getResponseCode());
return resultStr;
}
} catch (Exception e) {
e.printStackTrace();
return resultStr;
} finally {
if (out != null) {
out.close();
}
httpURLConnection.disconnect();
}
}
相似问题