为什么 Web 服务中的尾部斜杠如此重要?
我正在用 PHP 和 Python 测试 Web 服务。 假设 Web 服务的地址是 http://my.domain.com/my/webservice
。 当我使用该 URL 测试 PHP 中的 Web 服务时,一切正常。 但是,当我使用相同的位置但在 Python 中使用 SOAPpy 时,我收到了错误。
下面是我用来与 Web 服务 (Python) 通信的代码:
from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice', namespace)
server.myFunction()
我从服务器得到的响应:
HTTPError: <HTTPError 301 Moved Permanently>
我发现如果我向 Web 服务位置添加尾部斜杠,它就可以工作!
from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice/', namespace)
server.myFunction()
为什么缺少尾部斜杠会导致错误?
I was testing a web service in PHP and Python. The address of the web service was, let's say, http://my.domain.com/my/webservice
. When I tested the web service in PHP using that URL everything worked fine. But, when I used the same location but in Python using SOAPpy I got an error.
Below is the code I used to communicate with the web service (Python):
from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice', namespace)
server.myFunction()
The respond I got from the server:
HTTPError: <HTTPError 301 Moved Permanently>
I figure out that if I add a trailing slash to the web service location it works!
from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice/', namespace)
server.myFunction()
Why the lack of the trailing slash causes the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它们是不同的 URL。
http://my.domain.com/my/webservice
表示my
文件夹中存在一个文件webservice
。http://my.domain.com/my/webservice/
表示 my/webservice 文件夹内的默认文档。许多网络服务器会自动更正此类 URL,但这并不是必需的。
They're different URLs.
http://my.domain.com/my/webservice
implies a filewebservice
in themy
folder.http://my.domain.com/my/webservice/
implies the default document inside the my/webservice folder.Many webservers will automatically correct such URLs, but it is not required for them to do so.
因为实际的服务器 URL 是:
http://my.domain.com/my/webservice/
PHP 库必须是默认情况下遵循重定向。
Because the actual server URL is:
http://my.domain.com/my/webservice/
The PHP library must be following redirects by default.
该错误是 301 重定向,这意味着 Web 服务器将您重定向到末尾带有斜杠的 URL。
PHP 似乎会自动遵循此重定向,因此不会抛出错误,而 Python 不会。 您需要执行以下操作:
新的 URL 应在响应标头中可用。
HTH。
The error is a 301 redirect meaning the you are being redirected to the URL with the slash on the end by the web server.
It seems that PHP will auto follow this redirect and thus not throw the error, whereas Python won't. You will need to do the following:
The new URL should be available in the response headers.
HTH.
[免责声明:这是我的答案的副本来自此处。 我知道有些人不喜欢这种复制,但这解释了为什么斜杠很重要。]
页面
假设您提供一个包含单击时的
,用户的浏览器将检索
http://mydomain.com/more .html
。 如果您提供服务(使用相同的内容),浏览器将检索
http://mydomain.com/bla/more.html
。 为了避免这种歧义,如果 URL 指向目录,则重定向会附加斜杠。[Disclaimer: This is a copy of my answer from here. I know some people don't like this kind of copying, but this explains why the slash is important.]
Imagine you serve a page
that contains
On click, the user's browser would retrieve
http://mydomain.com/more.html
. Had you instead served(with the same content), the browser would retrieve
http://mydomain.com/bla/more.html
. To avoid this ambiguity, the redirection appends a slash if the URL points to a directory.SOAP-URL 的外观取决于服务器,是否需要斜杠取决于服务器和 SOAP 实现。
在您的例子中,我假设目标服务器是 apache 服务器,并且 SOAP URL 实际上是包含 SOAP 处理脚本的目录。
当您在服务器上访问 http://my.domain.com/my/webservice 时, apache 确定目录正确寻址为 http://my.domain.com/my/webservice/ 并发送 301 重定向。
SOAP 使用 http POST,由客户端决定是否应该遵循重定向,我认为它只是不期望这样。
SOAP 的其他实现(例如 Java 中的 Apache Axis)具有看起来像 Servlet 的 URL,例如 http://domain.com/没有斜线的soap/webservice,在这种情况下,没有斜线的URL是正确的,无论如何都不存在目录。
我认为,轴在重定向时也会失败。
What a SOAP-URL looks like is up to the server, if a slash is necessary depends on the server and the SOAP implementation.
In your case, I assume that the target server is an apache server and the SOAP URL is actually a directory that contains your SOAP handling script.
When you access http://my.domain.com/my/webservice on the server, apache decides that the directory is properly addressed as http://my.domain.com/my/webservice/ and sends a 301 redirect.
SOAP uses a http POST, its up to the client to decide if the redirect should be followed or not, I assume that it just doesn't expect one.
Other implementations of SOAP, e.g. Apache Axis in Java have URLs that look like Servlets, e.g. http://domain.com/soap/webservice without slash, in this case the URL without slash is correct, there is no directory that exists anyway.
Axis fails on redirects as well, I think.