作者:Cindere丷lla_茹满 | 来源:互联网 | 2023-05-18 21:30
前端:
/**
* 调用后台服务
*/
public static UpdateToolResponseBean callService(String url,UpdateToolRequestBean requestBean,String type) {
if (url==null||"".equals(url)) {
logger.error("callService() the url is null, return null!");
return null;
}
String re = null;
UpdateToolResponseBean respOnseBean= null;
ObjectMapper objectMapper = JacksonHelper.objectMapper();
try {
String requestJsOnStr= objectMapper.writeValueAsString(requestBean);
re = HttpConnectService.requestByHttpConn(url,requestJsonStr,type);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
logger.error("Server connection failed : "+e);
}
if (re != null) {
try {
respOnseBean= objectMapper.readValue(re, UpdateToolResponseBean.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseBean;
}
public static String requestByHttpConn(String url, String reqParms, String type) throws IOException {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
org.apache.http.client.HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost(url);
List formParams = new LinkedList();
formParams.add(new BasicNameValuePair("data", reqParms));
formParams.add(new BasicNameValuePair("type", type));
HttpEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
httppost.setEntity(entity);
HttpResponse respOnse= httpclient.execute(httppost);
logger.debug("statusCode is " + response.getStatusLine().getStatusCode());
HttpEntity resEntity = response.getEntity();
logger.debug("----------------------------------------");
logger.debug(response.getStatusLine());
if (resEntity != null) {
logger.debug("返回长度: " + resEntity.getContentLength());
logger.debug("返回类型: " + resEntity.getContentType());
InputStream in = resEntity.getContent();
logger.debug("in is " + in);
String inS = inputStream2String(in);
logger.debug(inS);
return inS;
}
return "";
}
private static String inputStream2String(InputStream in) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String tempbf;
StringBuffer out = new StringBuffer(4096);
while ((tempbf = br.readLine()) != null) {
out.append(tempbf);
}
return out.toString();
}
服务器端:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cOntent= request.getParameter(MsiConstants.KEY_DATA);
String type = request.getParameter(MsiConstants.DATA_TYPE);
try {
doAction(request, response, content, type);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
void doAction(HttpServletRequest httprequest,HttpServletResponse httpresponse, String content, String type)
throws Exception{
UpdateToolRequest requestBean = null;
JSONObject json = JSONObject.fromObject(content.trim());
requestBean = (UpdateToolRequest) JSONObject.toBean(json, UpdateToolRequest.class);
if (logger.isDebugEnabled()) {
logger.debug(requestBean.toString());
}
setRemoteIP("["+httprequest.getRemoteAddr()+"]");
if (ConstantUtils.MSI_UPDATE_ACTIVATE.equals(type)) {
response(httpresponse.getWriter(), service.activateClient(requestBean));
}
}
public void response(PrintWriter pw, Object response) {
try {
JSONObject json = JSONObject.fromObject(response);
pw.println(json.toString());
} catch (Exception e) {
logger.error(" response exception:", e);
String re = "{'errcode':'" + -1 + "'}";
pw.println(re);
}
}