热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Httpclient访问服务器

前端:***调用后台服务*publicstaticUpdateToolResponseBeancallService(Stringur

前端:

    /**
* 调用后台服务
*/

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);
}
}

推荐阅读
author-avatar
Cindere丷lla_茹满
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有