对接IPTV时,调试webservice接口时提示 no SOAPAction header! 异常 特此记录,提供两种解决方案
org.apache.axis.AxisFault: no SOAPAction header!
at org.apache.axis.transport.http.AxisServlet.getSoapAction(AxisServlet.java:586) [classes/:?]
at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:385) [classes/:?]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327) [axis-1.4.jar:?]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) [shiro-web-1.4.2.jar:1.4.2]
方案1 修改请求方的http请求头
我使用的Htool包提供的http请求根据,设置请求头SOAPAction
String xmlStr = "xml内容";
HttpRequest httpRequest = HttpRequest.post(url);
httpRequest.header("Content-Length", Integer.toString(xmlStr.length()));
httpRequest.header("Content-Type", "text/xml; charset=utf-8");
//关键参数 请求的接口 值可以为空
httpRequest.header("SOAPAction", "ExecCmd");
httpRequest.timeout(5000);
httpRequest.body(xmlStr.toString());
String body = httpRequest.execute().body();
方案2 修改接口提供方 AxisServlet 类
我们需要覆盖默认的AxisServlet类,在自己的工程里面创建 org.apache.axis.transport.http 包路径,然后创建AxisServlet.java 文件,将原始的AxisServlet类的代码全部复制到新的类中,注意 getSoapAction 方法我们需要进行一些修改
private String getSoapAction(HttpServletRequest req) throws AxisFault {
String soapAction = req.getHeader("SOAPAction");
if (soapAction == null) {
String contentType = req.getHeader("Content-Type");
if (contentType != null) {
int index = contentType.indexOf("action");
if (index != -1) {
soapAction = contentType.substring(index + 7);
}
}
//todo 去除请求头的 SOAPAction 校验
if (soapAction == null){
soapAction = "";
}
}
if (isDebug) {
log.debug("HEADER_SOAP_ACTION:" + soapAction);
}
if (soapAction == null) {
AxisFault af = new AxisFault("Client.NoSOAPAction", Messages.getMessage("noHeader00", "SOAPAction"), (String)null, (Element[])null);
exceptionLog.error(Messages.getMessage("genFault00"), af);
throw af;
} else {
if (soapAction.startsWith("\"") && soapAction.endsWith("\"") && soapAction.length() >= 2) {
int end = soapAction.length() - 1;
soapAction = soapAction.substring(1, end);
}
if (soapAction.length() == 0) {
soapAction = req.getContextPath();
}
return soapAction;
}
}