HttpURLConnection使用小记
// 调用百度OCR
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
// 用java JDK自带的URL去请求
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置该请求的消息头
// 设置HTTP方法:POST
connection.setRequestMethod(“POST”);
// 设置其Header的Content-Type参数为application/x-www-form-urlencoded
connection.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
// 填入apikey到HTTP header
//connection.setRequestProperty(“apikey”, “uml8HFzu2hFd8iEG2LkQGMxm”);
// 将第二步获取到的token填入到HTTP header
//connection.setRequestProperty(“access_token”, GetTocken.getAuth());
connection.setDoOutput(true);
//设置body中的参数
connection.getOutputStream().write(httpArg.getBytes(“UTF-8”));
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, “UTF-8”));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}