import java.io.BufferedReader;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.qcloud.image.sign.Credentials;
import com.qcloud.image.sign.Sign;
public class IsCard {
static int appId = ********;// YOUR_APPID
static String secretId = "********";
static String secretKey = "********";
static String bucketName = "********";
public static void main(String[] args) {
doJsonPost();
}
public static String doJsonPost() {
String result = "";
BufferedReader reader = null;
String respContent = null;
String urlimg = "********"; //图片地址
Credentials cred = new Credentials(appId, secretId, secretKey);
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://recognition.image.myqcloud.com/ocr/general");
String sign = Sign.appSign(cred,"**",300);//签名
post.addHeader("Authorization",sign);
String json1 = "{\"app_id\":\""+appId+"\",\"bucket\":\""+bucketName+"\",\"url\":\""+urlimg+"\"}"; //参数格式要正确
StringEntity entity = new StringEntity(json1.toString(),"utf-8");
entity.setContentType("application/json;charset=UTF-8");
post.setEntity(entity);
System.out.println(json1.toString());
String Json = json1.toString();
if (Json != null) {
byte[] writebytes = Json.getBytes();
// 设置文件长度
System.out.println(1);
}
HttpResponse resp = client.execute(post);
if(resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he,"UTF-8");
System.out.println(respContent);
}else{
Header[] heads = resp.getAllHeaders();
StringBuffer sb = new StringBuffer();
for (Header header : heads) {
sb.append("<p>")
.append(header.getName())
.append(" : ")
.append(header.getValue())
.append("</p>");
}
System.out.println(sb.toString());
System.out.println(resp.getStatusLine().getStatusCode());
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he,"UTF-8");
System.out.println(respContent);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}