http调接口

private static String doGetResult(String urlStr, Map<String, String> params)
throws Exception {
System.out.println(urlStr);
PostMethod postMethod = new PostMethod(urlStr);
try {
postMethod.addParameter("info",
URLEncoder.encode(Map2JSON(params), UTF8));
HttpClient client = new HttpClient();
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, UTF8);// 对含有中文的字符进行编码
client.setConnectionTimeout(1000 * 60); // 设置超时时间 1分钟
int status = 0;
status = client.executeMethod(postMethod);
if (status != 200) {// 连接失败
// System.out.println("responseMsg:服务器内部错误!!!");
throw new Exception("服务器内部错误!!!");
}
byte[] responseBody = postMethod.getResponseBody();
return new String(responseBody);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
} finally {
// 6.释放连接
postMethod.releaseConnection();
}
}

//map2json方法

private static String Map2JSON(Map<String, String> params) throws Exception {
Map<String, String> treeMap = new TreeMap<String, String>();
treeMap.putAll(params);
treeMap.put("sign", digest(getBeforeSign(treeMap).toString()));
return JSON.toJSONString(treeMap);
}

public static Map updatePassWord(String empName, String passWord,
String modiPerson) {
Map<String, String> params = new HashMap<String, String>();
params.put("empName", empName);
params.put("empPassword", passWord);
params.put("modiPerson", modiPerson);
Map map = new HashMap();
try {
String jsonStr = URLDecoder.decode(
doGetResult(URL_UPPASSWORD, params), "UTF-8");
map = JSON.parseObject(jsonStr, Map.class);
} catch (Exception e) {
e.printStackTrace();
map.put(RESP_STATUS, FAiL_SIGN);
map.put(RESP_MSG, e.getMessage());
}
return map;
}

原文地址:https://www.cnblogs.com/lizihao/p/5505822.html