/**
*测试新增图片
* @throws IOException
* @throws HttpException
* @throws SAXException
* @throws ParserConfigurationException
*/
// @Test
// public void TestUploadImage() throws HttpException, IOException, SAXException, ParserConfigurationException{
// System. out.println("----------");
// System.out.println("----------");
// HttpClient client = new HttpClient();
// PostMethod method = new PostMethod("http://localhost:8080/egis-scms-core/order/fileUpLoad.do");
// File file = new File("D:/a.jpg");
/ / FilePart fp = new FilePart("files", file);
// StringPart barcode = new StringPart("id", "GP1101MR3401S100277736");
// StringPart picCode = new StringPart("picCode", "GP17");
// Part[] parts = {id,picCode,fp};
// MultipartRequestEntity mre = new MultipartRequestEntity(parts,method.getParams());
// method.setRequestEntity(mre);
// int httpStat = client.executeMethod(method) ;
//
// if (httpStat != 200) {
// //如果失败,获取返回XML信息
// String xml = method.getResponseBodyAsString();
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder builder = factory.newDocumentBuilder();
// StringReader stringReader = new StringReader(xml);
// InputSource inputSource = new InputSource(stringReader);
// Document doc = builder.parse(inputSource);
// doc.normalize();
/ / NodeList resultNL = doc.getElementsByTagName("desciption");
// String fromAgentResult = resultNL.item(0).getFirstChild().getNodeValue();
// }
// }
//开始调用服务
Map result = null;
try {
result = (Map) HttpProxy.send(scmsCoreUrl+UrlConstants.pubOrderInsuranceListUrl, params, Map.class);
} catch (IOException e) {
LoggerUtil.logError(this.getClass() .getName(), "orderList", e);
throw new BusinessException(e);
}
工具类:
public class HttpProxy {
public static Object send(String url,SettingModel setting,Map<String,String> parameter,Class<?> resultClazz){
Object result = null;
LoggerUtil.trace(HttpProxy.class.getName(), "数据接收类:" , resultClazz==null?null:resultClazz.getName()+"请求地址: " + url + "请求参数: " + parameter );
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy() );
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
HttpUriRequest method = HttpMethodUtil.createMethod(url,setting,parameter,null);
HttpResponse resp = httpclient.execute(method);
String resultBody = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
int status = resp.getStatusLine(). getStatusCode();
LoggerUtil.trace(HttpProxy.class.getName(), "返回状态:" + status,"返回值:" + resultBody);
if(resultClazz == null || resultClazz.equals(String.class)) {
result = resultBody;
}else{
Resolve resolve = ResolveFactory.createResolve(setting.getDataType());
result = resolve.resolve(resultBody, resultClazz);
}
}catch(Exception e){
LoggerUtil.logError(HttpProxy.class.getName (), resultClazz==null?"":resultClazz.getName() +"请求地址: " + url + "请求参数: " + parameter ,e );
}
return result;
}
public static Object send(String url, String data, SettingModel setting, Class<?> resultClazz) throws BusinessException {
Object result = null;
LoggerUtil.trace(HttpProxy.class.getName(), "数据接收类:" ,"请求地址: " + url + "请求参数: " + data);
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
HttpUriRequest method = HttpMethodUtil.createMethod(url,setting,data,null);
HttpResponse resp = httpclient.execute(method);
String resultBody = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
int status = resp.getStatusLine().getStatusCode();
LoggerUtil.trace(HttpProxy.class.getName(), "返回状态:" + status,"返回值:" + resultBody);
if(resultClazz == null || resultClazz.equals(String.class)){
result = resultBody;
}else{
Resolve resolve = ResolveFactory.createResolve(setting .getDataType());
result = resolve.resolve(resultBody, resultClazz);
}
}catch(Exception e){
LoggerUtil.logError(HttpProxy.class.getName(), "数据接收类:" + resultClazz==null?null :resultClazz.getName() +"请求地址: " + url + "请求参数: " + data ,e );
throw new BusinessException();
}
return result;
}
public static Object sendAddHeader(String url,SettingModel setting,Map<String,String> parameter,String Authorization,Class<?> resultClazz){ Object result = null; //StringBuilder result2 = new StringBuilder(); LoggerUtil.trace(HttpProxy. class.getName(), "数据接收类:" , resultClazz==null?null:resultClazz.getName() + "请求地址: " + url + "请求参数: " + parameter + "Auth认证密钥" + Authorization ); try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( builder.build()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory( sslsf) .build();
// HttpUriRequest method = HttpMethodUtil.createMethod(url,setting,parameter,Authorization); HttpPost method = new HttpPost(url); if(Authorization != null && !"".equals(Authorization)){ method.addHeader("Authorization ", Authorization); } List<NameValuePair> param = new ArrayList<NameValuePair>(); Set<Entry<String,String>> entrySet = parameter.entrySet(); Iterator<Entry<String,String>> iterator = entrySet. iterator(); while(iterator.hasNext()){ Entry<String,String> entry = iterator.next(); param.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } method .setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
HttpResponse resp = httpclient.execute(method); String resultBody = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8); LoggerUtil.trace(HttpProxy.class.getName(), "返回值:" , resultBody); if (resultClazz == null || resultClazz.equals(String.class)){ }else{ Resolve resolve = ResolveFactory.createResolve(setting.getDataType()); result = resolve.resolve(resultBody, resultClazz); } }catch(Exception e){ LoggerUtil.logError(HttpProxy.class.getName(), resultClazz==null?null:resultClazz.getName() +"请求地址: " + url + "请求参数: " + parameter ,e ); } return result ; }
public static Object send(String url,Map<String,String> parameter,Class<?> resultClazz) throws IOException{
SettingModel setting = new SettingModel();
return send(url,setting,parameter,resultClazz);
}
}