首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Apache CXF在SOAP POST请求中获取错误,但curl工作正常

使用Apache CXF在SOAP POST请求中获取错误,但curl工作正常
EN

Stack Overflow用户
提问于 2015-06-15 14:10:49
回答 6查看 8.1K关注 0票数 25

我遇到了一个非常奇怪的问题。我正在尝试通过HTTPS执行SOAP POST请求。当我从我的代码中发送请求时,我得到了HTTP 500错误。现在,从cxf日志中,如果我复制了收到错误的相同SOAP消息(头和正文),并使用一个简单的curl请求对其进行post,它就可以正常工作。下面是我如何创建服务类和其他初始化

代码语言:javascript
复制
URL wsdlurl = SOAPWebServiceTransport.class.getClassLoader().
        getResource("my.wsdl");
OnlinePort service= new OnlinePortService(wsdlurl).getOnlinePortPort();
Client proxy = ClientProxy.getClient(service);

// Provides WS-Security
WSS4JOutInterceptor wss4jOut = new WSS4JOutInterceptor();
wss4jOut.setProperty("action", "UsernameToken");
wss4jOut.setProperty("user", userName);
wss4jOut.setProperty("passwordType", "PasswordText");
wss4jOut.setProperty("password", password);
wss4jOut.setProperty(WSHandlerConstants.ADD_UT_ELEMENTS,
        WSConstants.NONCE_LN + " " + WSConstants.CREATED_LN);
wss4jOut.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordCallback.class.getName());

proxy.getEndpoint().getOutInterceptors().add(wss4jOut);
setConduitProperties((HTTPConduit) proxy.getConduit(),url);

在set conduit方法中,我忽略了ssl检查(仅针对开发环境),并设置了一些头文件。

代码语言:javascript
复制
TLSClientParameters tcp = new TLSClientParameters();
tcp.setDisableCNCheck(true);
// Creating Trust Manager
TrustManager[] trustAllCerts = new TrustManager[] {
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }
} };

tcp.setTrustManagers(trustAllCerts);
conduit.setTlsClientParameters(tcp);

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setAllowChunking(false);
httpClientPolicy.setAccept("*/*");
httpClientPolicy.setContentType("text/xml;charset=UTF-8");
httpClientPolicy.setHost(url.split("/")[2]);
conduit.setClient(httpClientPolicy);

任何帮助都将是非常感谢的。

代码语言:javascript
复制
Response-Code: 500
Encoding: ISO-8859-1
Content-Type: text/html;charset=ISO-8859-1
Headers: {connection=[close], content-type=[text/html;charset=ISO-8859-1],
          Date=[Mon, 15 Jun 2015 06:42:09 GMT], Server=[Apache-Coyote/1.1],
          Set-Cookie=[JSESSIONID=FF0E4F5DCA42F700FFAC46BBD039FC20; Path=/; Secure],
          transfer-encoding=[chunked]}
Payload: 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

> <html> <head> <meta http-equiv="Content-Type" content="text/html;
> charset=ISO-8859-1"/> <title>Error Page</title> </head> <body>Invalid
> Request </body> </html>


at org.apache.cxf.interceptor.StaxInInterceptor.handleMessage(StaxInInterceptor.java:79)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:797)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1618)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1491)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1399)
    at org.apache.cxf.io.CacheAndWriteOutputStream.postClose(CacheAndWriteOutputStream.java:47)
    at org.apache.cxf.io.CachedOutputStream.close(CachedOutputStream.java:188)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:646)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:533)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
 Caused by: org.apache.cxf.interceptor.Fault: Response was of unexpected text/html ContentType.  Incoming portion of HTML stream: 

CURL请求

curl -k --header“内容类型:文本/xml;字符集=UTF-8”--header "SOAPAction:“--data @soaprequest.xml https://url

Curl log in verbose (当然,我已经更改了一些url端口names0

来自服务器的错误响应

代码语言:javascript
复制
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error Page</title>
</head>
<body>Invalid Request
</body>
</html> 
EN

回答 6

Stack Overflow用户

发布于 2018-03-28 11:34:03

SOAP在xml上工作,我可以看到您有以下错误

Caused by: org.apache.cxf.interceptor.Fault: Response was of unexpected text/html ContentType. Incoming portion of HTML stream:

这表示您的响应是以HTML、Response-Code: 500 Encoding: ISO-8859-1 Content-Type: text/html;chars

因此使用SOAP尝试使用WebService时可能会出现问题

此外,这个链接将是非常有帮助的讨论相同的问题。https://forums.mulesoft.com/questions/27468/web-service-consumer-response-was-of-unexpected-te.html

票数 1
EN

Stack Overflow用户

发布于 2015-07-18 15:19:47

如果您无法访问服务器上的日志,可以尝试检查您的代码请求和curl请求是否不同。

为此,您可以使用像tcpmon这样的嗅探器来嗅探http请求。

Tcpmon作为客户端和服务器之间的代理运行。它拦截所有请求并打印每个请求和响应的详细信息。

票数 0
EN

Stack Overflow用户

发布于 2015-11-10 22:48:01

转变是使用核心java HttpURLConnection来实现这一点。

一个类似这样的方法帮助事业在没有任何框架的情况下发布了一个any服务调用:

代码语言:javascript
复制
public HttpURLConnection getHttpConn(String webservice_url) throws IOException {
URL endpoint = new URL(webservice_url);
URLConnection connection = endpoint.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
byte[] encodedBytes = Base64.encodeBase64((getUsername()+":"+getPassword()).getBytes());
httpConn.setRequestMethod(getRequestMethod());
httpConn.setRequestProperty(HTTP_ACCEPT_ENCODING, getAccept_Encoding());
httpConn.setRequestProperty(HTTP_CONTENT_TYPE, getContentType());
httpConn.setRequestProperty(getContent_Length(), getContent_Length());
httpConn.setRequestProperty(HTTP_HOST, getHost());
httpConn.setRequestProperty(getConnection(), getConnection());
httpConn.setRequestProperty(HTTP_COOKIE2, getCookie2());
httpConn.setRequestProperty(HTTP_COOKIE, getCookie());
httpConn.setRequestProperty("Authorization", "Basic "+new String(encodedBytes));
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
return httpConn;
}

确保在CXF配置文件(cxf.xml)中配置http客户端。

如Apache CXF文档中所述:

1.添加http-conduit名称空间和xsd:

代码语言:javascript
复制
<beans ...
       xmlns:http-conf="http://cxf.apache.org/transports/http/configuration
       ...
       xsi:schemaLocation="...
           http://cxf.apache.org/transports/http/configuration
           http://cxf.apache.org/schemas/configuration/http-conf.xsd
       ...">

2.添加http-conduit标签/元素,并将RecieveTimeout/ConnectionTimeout设置为180000ms:

代码语言:javascript
复制
<http-conf:conduit name="*.http-conduit">
      <http-conf:client 
                      ConnectionTimeout="300000"
                      ReceiveTimeout="300000"/>       
</http-conf:conduit>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30838382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档