新增访问Topgp Webservice接口Demo
This commit is contained in:
parent
69d476e628
commit
2723b5cd14
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.ruoyi.test.conrtroller;
|
||||||
|
|
||||||
|
import com.ruoyi.common.utils.XmlUtils;
|
||||||
|
import com.ruoyi.common.utils.http.HttpUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
|
||||||
|
public class XmlWebserviceController {
|
||||||
|
//private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
||||||
|
|
||||||
|
@PostMapping("/anon/sendXml")
|
||||||
|
public String SendXml() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("responseInfo", "此处为测试消息");
|
||||||
|
String param = XmlUtils.GetTopgpRequestXml("express_testRequest", map);
|
||||||
|
String url = "http://192.168.2.81:85/web/ws/r/aws_ttsrv2_toptest";
|
||||||
|
String returnXml = HttpUtils.sendXmlPost(url,param);
|
||||||
|
return XmlUtils.GetStatusFromTopgpResponse(returnXml).toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -101,6 +101,13 @@
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20160810</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
|
import com.ruoyi.common.utils.http.HttpUtils;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.json.XML;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class XmlUtils {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组合TOPGP所需的XML格式
|
||||||
|
* @param tip 调用TOPGP的Webservice的方法名 如:express_testRequest
|
||||||
|
* @param mapInfo XML中Filed对应的键值对Map
|
||||||
|
* @return xml字符串
|
||||||
|
*/
|
||||||
|
public static String GetTopgpRequestXml(String tip, Map<String,Object> mapInfo){
|
||||||
|
log.info("=======生成xml======");
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
stringBuffer.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tip=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\">\n" +
|
||||||
|
" <soapenv:Header/>\n" +
|
||||||
|
" <soapenv:Body>\n" +
|
||||||
|
" <tip:"+tip+">\n" +
|
||||||
|
" <tip:request>\n" +
|
||||||
|
" <Request>\n" +
|
||||||
|
" <Access>\n" +
|
||||||
|
" <Authentication user='topgui' password='' />\n" +
|
||||||
|
" <Connection application='bps' source='www.bpsemi.com' />\n" +
|
||||||
|
" <Organization name='SYSTEM' />\n" +
|
||||||
|
" <Locale language='zh_cn' />\n" +
|
||||||
|
" </Access>\n" +
|
||||||
|
" <RequestContent>\n" +
|
||||||
|
" <Parameter>\n" +
|
||||||
|
" <Record>\n" +
|
||||||
|
" <Field name=");
|
||||||
|
for(String key:mapInfo.keySet()){
|
||||||
|
stringBuffer.append("'"+key+"' value='" +mapInfo.get(key).toString().replaceAll("&","&")+"' />\n");
|
||||||
|
}
|
||||||
|
stringBuffer.append(" </Record>\n" +
|
||||||
|
" </Parameter>\n" +
|
||||||
|
" <Document/>\n" +
|
||||||
|
" </RequestContent>\n" +
|
||||||
|
" </Request>\n" +
|
||||||
|
" </tip:request>\n" +
|
||||||
|
" </tip:express_testRequest>\n" +
|
||||||
|
" </soapenv:Body>\n" +
|
||||||
|
"</soapenv:Envelope>");
|
||||||
|
log.info("=======生成xml结束======");
|
||||||
|
return stringBuffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将TOPGP返回的XML转化为Json,并提出返回Status
|
||||||
|
* @param TopgpResonseXml 调用TOPGP的Webservice的方法名 如:express_testRequest
|
||||||
|
* @return Status JsonObject
|
||||||
|
*/
|
||||||
|
public static JSONObject GetStatusFromTopgpResponse(String TopgpResonseXml) {
|
||||||
|
JSONObject jsonObject = XML.toJSONObject(TopgpResonseXml);
|
||||||
|
|
||||||
|
JSONObject envelope = jsonObject.getJSONObject("SOAP-ENV:Envelope");
|
||||||
|
JSONObject body = envelope.getJSONObject("SOAP-ENV:Body");
|
||||||
|
JSONObject express_testResponse = body.getJSONObject("fjs1:express_testResponse");
|
||||||
|
JSONObject fjs1Response = express_testResponse.getJSONObject("fjs1:response");
|
||||||
|
JSONObject response = fjs1Response.getJSONObject("Response");
|
||||||
|
JSONObject execution = response.getJSONObject("Execution");
|
||||||
|
return execution.getJSONObject("Status");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
package com.ruoyi.common.utils.http;
|
package com.ruoyi.common.utils.http;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import java.io.IOException;
|
import org.slf4j.Logger;
|
||||||
import java.io.InputStream;
|
import org.slf4j.LoggerFactory;
|
||||||
import java.io.InputStreamReader;
|
import org.springframework.http.ResponseEntity;
|
||||||
import java.io.PrintWriter;
|
import org.springframework.web.client.RestClientException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.net.ssl.*;
|
||||||
|
import java.io.*;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
@ -12,18 +16,6 @@ import java.net.URLConnection;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.net.ssl.HostnameVerifier;
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
|
||||||
import javax.net.ssl.SSLContext;
|
|
||||||
import javax.net.ssl.SSLSession;
|
|
||||||
import javax.net.ssl.TrustManager;
|
|
||||||
import javax.net.ssl.X509TrustManager;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import com.ruoyi.common.constant.Constants;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.client.RestClientException;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用http发送方法
|
* 通用http发送方法
|
||||||
|
|
@ -265,12 +257,72 @@ public class HttpUtils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送xml POST方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*Author yangbo
|
||||||
|
*/
|
||||||
|
public static String sendXmlPost(String url, String param) {
|
||||||
|
PrintWriter out = null;
|
||||||
|
BufferedReader in = null;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try {
|
||||||
|
String urlNameString = url;
|
||||||
|
log.info("sendPost - {}", urlNameString);
|
||||||
|
URL realUrl = new URL(urlNameString);
|
||||||
|
URLConnection conn = realUrl.openConnection();
|
||||||
|
conn.setRequestProperty("accept", "*/*");
|
||||||
|
conn.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
conn.setRequestProperty("Accept-Charset", "utf-8");
|
||||||
|
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); //发送xml需加上此请求头
|
||||||
|
conn.addRequestProperty("SOAPAction", "\"\""); //向topgp发送xml必须加上该Name=“SOAPAction", Value="\"\"" ,否则会报415错误,不能识别XML.
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
out = new PrintWriter(conn.getOutputStream());
|
||||||
|
out.print(param);
|
||||||
|
out.flush();
|
||||||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
log.info("recv - {}", result);
|
||||||
|
} catch (ConnectException e) {
|
||||||
|
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
||||||
|
} catch (SocketTimeoutException e) {
|
||||||
|
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString().replace("<","<").replace(">",">");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向指定 Restful接口 发送POST方法的请求
|
* 向指定 Restful接口 发送POST方法的请求
|
||||||
*
|
*
|
||||||
* @param url 发送请求的 URL
|
* @param url 发送请求的 URL
|
||||||
* @param params 请求参数,请求参数为json的形式。例:params="{\"params\":{\"pagesize\":1000}}"
|
* @param params 请求参数,请求参数为json的形式。例:params="{\"params\":{\"pagesize\":1000}}"
|
||||||
* @return 返回Map, Key="statusCode",接口访问返回状态, key="result":接口返回接果
|
* @return 返回Map, Key="statusCode",接口访问返回状态, key="result":接口返回接果
|
||||||
|
*
|
||||||
|
* author yangbo
|
||||||
*/
|
*/
|
||||||
//public static Map<String,String> sendPostWithRest(String url, String params){
|
//public static Map<String,String> sendPostWithRest(String url, String params){
|
||||||
//如果参数为String类型,推送企业微信消息会乱码,因此改为Object类型,直接推送Map<Sring,Object> --yangbo 20210729
|
//如果参数为String类型,推送企业微信消息会乱码,因此改为Object类型,直接推送Map<Sring,Object> --yangbo 20210729
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue