Post 请求方式
public static String doPost(String url, String params, String token) throws Exception {
log.info("invoke url:"+url+" \nparams:"+params + " \ntoken:"+token);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
if(!"".equals(token)) {
httpPost.setHeader("Authorization", token);
}
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
int state = 999;
String jsonString = "";
try {
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
} else{
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
log.info("jsonString:"+jsonString + " 请求返回状态:" + state + "("+url+")");
}
} catch(Exception e) {
e.printStackTrace();
log.info("url:"+url+" params:"+params+" Exception:"+e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonString;
}
Get 请求方式
public static String doGet(String url, String param) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
builder.addParameter("wsdl", param);
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
delete 请求方式
public static String doDelete(String url, String params, String token) throws Exception {
log.info("invoke url:"+url+" \nparams:"+params + " \ntoken:"+token);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDeleteWithBody httpDel = new HttpDeleteWithBody(url);
httpDel.setHeader("Content-type", "application/json; charset=utf-8");
String jsonString = "";
int state = 999;
try {
if (null != params) {
StringEntity entity = new StringEntity(params, ContentType.APPLICATION_JSON);
httpDel.setEntity(entity);
}
CloseableHttpResponse response = httpclient.execute(httpDel);
StatusLine status = response.getStatusLine();
state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
} else{
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
log.info("jsonString:"+jsonString + " 请求返回状态:" + state + "("+url+")");
}
} catch (IOException e) {
log.info("delete请求提交失败:" + url, e);
} finally {
httpDel.releaseConnection();
}
return jsonString;
}
webservice 接口
public static String send(String params,String url,String method) {
log.info("===传递的参数==="+params);
log.info("===请求路径==="+url);
String result="";
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = null;
try {
try {
call = service.createCall();
} catch (javax.xml.rpc.ServiceException e) {
e.printStackTrace();
}
} catch (ServiceException e) {
e.printStackTrace();
}
call.setTargetEndpointAddress(url);
call.addParameter("in0",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
try {
result = (String) call.invoke(QName.valueOf(method), new Object[]{params});
} catch (Exception e) {
e.printStackTrace();
throw new NrmsYnException("接口异常");
}
return result;
}
webService 远程调用 soapClient 方法
public static String soapClient(String wsdlUrl,String params,String method) {
log.info("===传递的参数==="+params);
log.info("===请求路径==="+wsdlUrl);
log.info("===请求接口的方法名==="+method);
String result="";
SoapClient soapClient = SoapClient.create(wsdlUrl)
.setMethod(method, "http://WebXml.com.cn/")
.setParam("theCityName", params)
.setConnectionTimeout(15000)
.setReadTimeout(15000)
.timeout(15000);
result = soapClient.send();
System.out.println(result);
return result;
}