(首先,请耐心等待,这是我第一次使用各自的技术;也许这是一个非常简单的错误)
更新:我正在重述这个问题,因为我现在对这个问题了解得更多,问题显然在EXPath方面......
我试图让使用XSLT + EXPath HTTP客户端的客户端与服务器通信,但我无法使POST + <http:body>
组合起作用...
以下内容主要来自实际的示例代码,但找不到直接适用的示例并不容易。
以下是我使用的一些资源:
http://expath.org/modules/http-client/
http://expath.org/modules/http-client/samples
http://expath.org/spec/http-client
http://www.ibm.com/developerworks/library/x-expath/
我正在使用expath-http-client-saxon-0.12.0
,顺便说一句。
这是来自客户的代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:http="http://expath.org/ns/http-client"
xmlns:h="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all"
version="2.0">
<xsl:import href="http://expath.org/ns/http-client.xsl"/>
<xsl:output indent="yes"/>
<xsl:template name="main" match="/">
<root>
<!-- the request element -->
<xsl:variable name="request" as="element(http:request)">
<http:request method="post" href="http://127.0.0.1:8888/test">
<http:body media-type="application/xml"/>
</http:request>
</xsl:variable>
<xsl:variable name="body" as="element()">
<x>1</x>
</xsl:variable>
<!-- sending the request -->
<xsl:variable name="resp" select="http:send-request($request,(),$body)"/>
<!-- add the http:response element to the output tree -->
<xsl:sequence select="$resp[1]"/>
</root>
</xsl:template>
</xsl:stylesheet>
请原谅我,如果有些事情是多余的,我尝试了很多这方面的变种。
正如@MartinHonnen所建议的那样,我试图用Fiddler测试发送请求。
如果我用
打电话给客户saxon -xsl:post-example.xsl -it:main
然后我收到消息
May 15, 2016 10:53:42 AM org.expath.pkg.repo.tools.Logger info
INFO: Create a new repository with storage: File system storage in /usr/share/expath/repo
No license file found - running with licensable features disabled
2016/05/15 10:53:42:496 CEST [INFO] DefaultHttpClient - I/O exception (java.io.IOException) caught when processing request to {}->http://127.0.0.1:8888: Error serializing the body content
2016/05/15 10:53:42:499 CEST [INFO] DefaultHttpClient - Retrying request to {}->http://127.0.0.1:8888
2016/05/15 10:53:42:499 CEST [INFO] DefaultHttpClient - I/O exception (java.io.IOException) caught when processing request to {}->http://127.0.0.1:8888: Error serializing the body content
2016/05/15 10:53:42:500 CEST [INFO] DefaultHttpClient - Retrying request to {}->http://127.0.0.1:8888
2016/05/15 10:53:42:500 CEST [INFO] DefaultHttpClient - I/O exception (java.io.IOException) caught when processing request to {}->http://127.0.0.1:8888: Error serializing the body content
2016/05/15 10:53:42:500 CEST [INFO] DefaultHttpClient - Retrying request to {}->http://127.0.0.1:8888
Error on line 18 of post-example.xsl:
Error sending the HTTP request
Error sending the HTTP request
我试图了解如何配置序列化,但它非常复杂,我真的不认为默认值不会导致这么简单的问题?
Fiddler表明了这一点:
POST /test HTTP/1.1
Content-Type: application/xml; charset=UTF-8
Transfer-Encoding: chunked
Host: 127.0.0.1:8888
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_101)
但没有身体内容。
它还提供了大量
形式的消息96 Failed to obtain request body. System.IO.InvalidDataException Bad request: Chunked Body was missing entirely.
我也尝试了这个,结果相同:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:http="http://expath.org/ns/http-client"
xmlns:h="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all"
version="2.0">
<xsl:import href="http://expath.org/ns/http-client.xsl"/>
<xsl:output indent="yes"/>
<xsl:template name="main" match="/">
<root>
<!-- the request element -->
<xsl:variable name="request" as="element(http:request)">
<http:request method="post" href="http://127.0.0.1:8888/test">
<http:body media-type="text/plain">foo</http:body>
</http:request>
</xsl:variable>
<!-- sending the request -->
<xsl:variable name="resp" select="http:send-request($request)"/>
<!-- add the http:response element to the output tree -->
<xsl:sequence select="$resp[1]"/>
</root>
</xsl:template>
</xsl:stylesheet>
我怎样才能找出问题所在?
PS:如果有人可以建议使用另一种工具来连接XSLT(Saxon)和REST API,我很高兴听到它。答案 0 :(得分:0)
答案似乎很简单:使用expath-http-client-saxon-0.11.0
。
来自Adam Retter的建议来自
https://groups.google.com/forum/#!forum/expath
使用该版本可行。
因此,似乎版本0.12.0已被打破(目前)。