WebSphere部署工程HttpClient冲突
was8.5.项目里是httpclient-4.3.5和httpcore-4.3.2.在tomcat里面跑项目发送http请求没有问题,到了部署was后httpclient就不行,怎么也不能调用发送请求;
后面看到网上说有jar包冲突,从这点着手解决;
1:解决办法如下
替换掉之前引用的httpclient-4.3.5,换成commons-httpclient-3.0.1.jar;
代码如下:
protected static final String DEF_HTTP_ENCODE = "UTF-8";
protected static final String DEF_TCP_ENCODE = "UTF-8";
protected static int DEF_MMS_TCP_RECEIVEMSG = 10;
protected static int DEF_MMS_TCP_HEADLEN = 10;
public static String sendHttpReq(String uri, Map<String, String> paMap,
boolean isPost, String encode) throws Exception {
uri = StringUtils.trimToNull(uri);
if (uri == null) {
return null;
}
encode = DEF_HTTP_ENCODE;
HttpClient client = new HttpClient();
HttpMethod method = null;
int len = paMap == null ? 0 : paMap.size();
if (isPost) {
method = new PostMethod(uri);
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=" + encode);
// method.addRequestHeader("Content-Type","application/json;charset="
// + encode);
// method.setRequestHeader("Accept", "application/json;charset=" +
// encode);
if (len > 0) {
NameValuePair[] params = new NameValuePair[len];
int ix = -1;
Set<Entry<String, String>> ets = paMap.entrySet();
for (Entry<String, String> et : ets) {
ix++;
params[ix] = new NameValuePair(et.getKey(), et.getValue());
}
((PostMethod) method).setRequestBody(params);
}
} else {
if (len > 0) {
Set<Entry<String, String>> ets = paMap.entrySet();
boolean flag = false;
boolean uriHasPa = uri.indexOf("?") > 0;
for (Entry<String, String> et : ets) {
String val = StringUtils.trimToNull(et.getValue());
val = val == null ? "" : URLEncoder.encode(val, encode);
uri += (!uriHasPa && !flag ? "?" : "&") + et.getKey() + "="+ val;
flag = true;
}
}
method = new GetMethod(uri);
method.addRequestHeader("Content-Type", "text/html; charset=" + encode);
}
client.executeMethod(method);
return new String(method.getResponseBody(), encode);
}
public static void main(String[] args) {
String pwd = "111111";
JSONObject params2 = new JSONObject();
try {
params2.put("Type", "2");
params2.put("No", "0000000027");
params2.put("Pwd", com.kiosk.util.MD5.bytesToMD5(pwd.getBytes()));
Map<String, String> paMap = new HashMap<String, String>();
paMap.put("CustomerInfo", params2.toString());
String str = sendHttpReq("http://127.0.0.1:9080/manage/!verifyCustomer.do",paMap, true, "utf-8");
System.out.println("返回:" + str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}