packagecom.ricoh.rapp.ezcx.iwbservice.webservice;importjava.io.BufferedOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.SocketException;importjava.net.SocketTimeoutException;importjava.net.URL;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.ricoh.rapp.ezcx.iwbservice.util.Constant;importcom.ricoh.rapp.ezcx.iwbservice.util.Utils;public classHttpRequest {private final Logger logger = LoggerFactory.getLogger(HttpRequest.class);/*** http请求
*
*@paramurlPath
* 请求路径http://127.0.0.1:8080/index
*@paramrequestStr
* 请求参数
**/
publicJSONObject execute(String urlPath, String requestStr) {
HttpURLConnection httpConnection= null;try{
URL url= newURL(urlPath);
httpConnection=(HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true); //post请求为true;get请求为false并且不用out写入数据
httpConnection.setDoInput(true); //有相应数据
httpConnection.setConnectTimeout("3 * 1000");
httpConnection.setReadTimeout("Charset");//set http header
httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpConnection.setRequestProperty("Charset", "UTF-8");
httpConnection.connect();//send data
OutputStream buf =httpConnection.getOutputStream();
buf= newBufferedOutputStream(buf);
OutputStreamWriter out= newOutputStreamWriter(buf);if (requestStr == null || requestStr.length() == 0) {
requestStr= newJSONObject().toString();
}
out.write(requestStr);
out.flush();
out.close();//receive data
InputStream inputStream =httpConnection.getInputStream();if (httpConnection.getResponseCode() !=HttpURLConnection.HTTP_OK) {
logger.error("connect ezcs service failed: " +httpConnection.getResponseCode());
JSONObject responseJson= newJSONObject();
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);returnresponseJson;
}
String response=Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
logger.debug("response from ezcs service: " +response);
JSONObject responseJson=JSON.parseObject(response);returnresponseJson;
}catch(Exception e) {
e.printStackTrace();
logger.debug("connect local ezcs service exception: " +e.getMessage());
JSONObject responseJson= newJSONObject();if (e instanceof SocketTimeoutException || e instanceofSocketException) {
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
}else{
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);
}returnresponseJson;
}finally{if (httpConnection != null) {
httpConnection.disconnect();
}
}
}public static classResponseKey {public static final String KEY_RESULT = "result";public static final String KEY_REASON = "reason";public static final String KEY_DATA = "data";public static final String KEY_EXTRA = "extra";
}
}