用SOAP方式呼叫別人的介面
註明:本文的環境是在SSM框架下進行的。文中的地址是隨便寫的,僅做舉例使用
(1)application.properties檔案
application.DWWebService = http://180.230.40.31:6080/CRMInterface.asmx
(2)編寫ApplicationConfig類
@Configuration
@PropertySource(value="classpath:application.properties",encoding="utf-8")
public class ApplicationConfig {
@Value("${application.DWWebService}")
private String applicationDWWebService;
public String getApplicationDWWebService() {
return applicationDWWebService;
}
public void setApplicationDWWebService(String applicationDWWebService) {
this.applicationDWWebService = applicationDWWebService;
}
}
(3)編寫WebServiceUtil類
@Component
public class WebServiceUtil {
private static ApplicationConfig applicationConfig;
@Autowired
public WebServiceUtil(ApplicationConfig applicationConfig) {
WebServiceUtil.applicationConfig = applicationConfig;
}
/**
* 請求頭生成
* @return
*/
private static String getSoapHeader(){
StringBuffer soapHeader = new StringBuffer();
soapHeader.append("<soapenv:Header>");
soapHeader.append("<q0:Header>");
soapHeader.append("<q0:UserName>test</q0:UserName>");
soapHeader.append("<q0:PassWord>test1234</q0:PassWord>");
soapHeader.append("</q0:Header>");
soapHeader.append("</soapenv:Header>");
return soapHeader.toString();
}
//呼叫對方介面的XML
public static String createCustomerSyncXml(AjaxJson json) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", json.getCode());
jsonObject.put("message", json.getMessage());
jsonObject.put("attributes", json.getAttributes());
StringBuffer xml = new StringBuffer();
xml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://tempuri.org/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
xml.append(getSoapHeader());
xml.append("<soapenv:Body>");
xml.append("<q0:TSOrderWeb_CustomerSync>");
xml.append("<q0:CustomerJson>");
xml.append(jsonObject);
xml.append("</q0:CustomerJson>");
xml.append("</q0:TSOrderWeb_CustomerSync>");
xml.append("</soapenv:Body>");
xml.append("</soapenv:Envelope>");
return xml.toString();
}
//解析XML,獲取呼叫介面的返回值
public static Map<String, String> syncCrmDealerToM3(AjaxJson json) {
Map<String, String> resultMap = new HashMap<String, String>();
resultMap.put("result", "");
resultMap.put("errorMsg", "");
String xml = createCustomerSyncXml(json);
Document document = WebServiceUtil.parse2Document(
WebServiceUtil.sendXML(applicationConfig.getApplicationDWWebService(), xml));
if (document != null) {
Element rootElement = document.getRootElement();
if (rootElement != null) {
Element body = rootElement.element("Body");
if (body != null) {
Element responseElement = body.element("TSOrderWeb_CustomerSyncResponse");
if (responseElement != null) {
Element resultElement = responseElement.element("result");
Element errorMsgElement = responseElement.element("errorMsg");
if (resultElement != null) {
resultMap.put("result", resultElement.getTextTrim());
}
if (errorMsgElement != null) {
resultMap.put("errorMsg", errorMsgElement.getTextTrim());
}
}
}
}
}
return resultMap;
}
/**
* 生成解析文件
*
* @param xmlStr
* 解析的XML物件
* @return 返回一個document物件
*
*/
public static Document parse2Document(String xmlStr) {
Document document = null;
try {
document = DocumentHelper.parseText(xmlStr);
} catch (DocumentException e) {
log.error("讀取classpath下xmlFileName檔案發生異常,請檢查CLASSPATH和檔名是否存在!", e);
}
return document;
}
/**
*
* 呼叫介面,傳送xml文件獲取資料
*
* @param url
* 請求url
* @param soapAction
* 請求action
* @param xml
* XML請求文件
* @return 用車介面系統提供的XML資料
*/
public static String sendXML(String url, String xml) {
HttpURLConnection conn = null;
InputStream in = null;
InputStreamReader isr = null;
OutputStream out = null;
StringBuffer result = null;
try {
byte[] sendbyte = xml.getBytes("UTF-8");
URL connUrl = new URL(url);// 連線RUL
conn = (HttpURLConnection) connUrl.openConnection();
conn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
conn.setRequestProperty("Content-Length", sendbyte.length + "");// 設定檔案長度
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(1000 * 30);// 設定連線超時30000ms
conn.setReadTimeout(60000);// 設定讀取資料超時
out = conn.getOutputStream();
out.write(sendbyte);
if (conn.getResponseCode() == 200) {
result = new StringBuffer();
in = conn.getInputStream();
isr = new InputStreamReader(in, "UTF-8");
char[] c = new char[1024];
int a = isr.read(c);
while (a != -1) {
result.append(new String(c, 0, a));
a = isr.read(c);
}
}
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
return "";
} catch (IOException e) {
log.error(e.getMessage(), e);
return "";
} finally {
if (conn != null) {
conn.disconnect();
}
try {
if (in != null) {
in.close();
}
if (isr != null) {
isr.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result == null ? "" : result.toString();
}
}
(4)在controller層的方法中呼叫syncCrmDealerToM3()方法,傳入相應的引數即可