import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* Req_Mode_WebService 请求模式
*
* @author wst(shi-tao.wen) 2021年9月13日 上午10:20:43
*
*/
public class ReqModeWebService {
public static boolean test = false;
private Logger logger = LoggerFactory.getLogger(this.getClass());
public ReqModeWebService() { }
* WebService服务请求
*
* @author wst(shi-tao.wen) 2021年9月3日 上午10:57:28
* @param apiParam
* @return
*/
public GeneralApiResult WebServiceFun(GeneralApiParamInfo apiParam) { logger.info("WebServiceFun --> "); GeneralApiResult result = new GeneralApiResult();
HttpURLConnection connect = null;
DataOutputStream out = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(apiParam.getActualServerUrl());
connect = (HttpURLConnection) url.openConnection();
connect.setDoOutput(true);
connect.setDoInput(true);
connect.setRequestMethod("POST"); connect.setRequestProperty("SOAPAction", apiParam.getNamepace() + apiParam.getActualInteface()); connect.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); connect.setRequestProperty("content-length", apiParam.getParams().toString().getBytes("UTF-8").length + "");
out = new DataOutputStream(connect.getOutputStream());
out.write(getXML(apiParam.getActualInteface(), apiParam.getParams().toString()).getBytes("UTF-8")); out.flush();
out.close();
int code = connect.getResponseCode();
String tempString = null;
StringBuffer sb = new StringBuffer();
if (code == HttpURLConnection.HTTP_OK) { bufferedReader = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8"));
} else { bufferedReader = new BufferedReader(new InputStreamReader(connect.getErrorStream(), "UTF-8"));
}
while ((tempString = bufferedReader.readLine()) != null) { sb.append(tempString);
}
if (null != bufferedReader) { bufferedReader.close();
}
String websn = sb.toString();
connect.disconnect();
if (GeneralStringUtils.isBlank(websn)) { result.setMsg("接口请求异常,返回信息为空"); result.setCode(500);
result.setData("接口请求异常,返回信息为空"); } else { String start = "<" + apiParam.getActualInteface() + "Return>";
String end = "</" + apiParam.getActualInteface() + "Return>";
String responseXml = websn.substring(websn.indexOf(start) + start.length(), websn.lastIndexOf(end)).replaceAll("& lt;", "<").replaceAll("& gt;", ">"); result.setMsg("接口请求成功"); result.setCode(200);
result.setData(decodeUnicode(responseXml));
}
} catch (Exception e) { result.setCode(500);
result.setData(""); result.setMsg("系统请求异常,请求超时或业务异常,请稍后再试" + (e.getMessage() == null ? "" : ("," + e.getMessage()))); logger.info(result.getMsg());
if (e.getCause() != null && e.getCause().toString().toLowerCase().contains("time") && e.getCause().toString().toLowerCase().contains("out")) { }
e.printStackTrace();
} finally { if (out != null) { try { out.close();
} catch (Exception e) { e.printStackTrace();
}
}
if (bufferedReader != null) { try { bufferedReader.close();
} catch (Exception e) { e.printStackTrace();
}
}
if (connect != null) { try { connect.disconnect();
} catch (Exception e) { e.printStackTrace();
}
}
}
return result;
}
public static void main(String[] args) { String xml = "<操作*>取认证相片</操作*><用户名*>99999</用户名*><密码*>99999</密码*>";
GeneralApiParamInfo apiParam = new GeneralApiParamInfo();
apiParam.setParams(getXML("allDsjk", xml)); apiParam.setActualServerUrl("http://*.*.*.*/Service?wsdl"); apiParam.setActualInteface("allDsjk"); apiParam.setNamepace("http://***.wei.com/"); GeneralApiResult result = new ReqModeWebService().WebServiceFun(apiParam);
System.out.println(decodeUnicode(result.getData().toString()));
}
public static String getXML(String fun, String message) {
String xml = message.replaceAll("<", "& lt;").replaceAll(">", "& gt;"); String soapXML =
"<?xml version=\"1.0\" encoding=\"utf-16\"?>"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+ "<soap:Body>"
+ "<" + fun + " xmlns=\"http://server.wei.com\">"
+ "<xml>" + xml + "</xml>"
+ "</" + fun + ">" + "</soap:Body>"
+ "</soap:Envelope>";
return soapXML;
}
* &#x转码
* @author wst 2022年10月20日 下午4:27:05
* @param sourceString
* @return
*/
public static String decodeUnicode(String sourceString) {
Pattern compile = Pattern.compile("&#.*?;");
Matcher matcher = compile.matcher(sourceString);
while (matcher.find()) { String group = matcher.group();
String hexcode = "0" + group.replaceAll("(&#|;)", "");
sourceString = sourceString.replaceAll(group, (char) Integer.decode(hexcode).intValue() + "");
}
return sourceString;
}
}