为什么 Web 服务中的尾部斜杠如此重要?

发布于 2024-07-27 22:31:04 字数 661 浏览 5 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转身泪倾城 2024-08-03 22:31:04

它们是不同的 URL。 http://my.domain.com/my/webservice 表示 my 文件夹中存在一个文件 webservicehttp://my.domain.com/my/webservice/ 表示 my/webservice 文件夹内的默认文档。

许多网络服务器会自动更正此类 URL,但这并不是必需的。

They're different URLs. http://my.domain.com/my/webservice implies a file webservice in the my 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.

时光暖心i 2024-08-03 22:31:04

因为实际的服务器 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.

再可℃爱ぅ一点好了 2024-08-03 22:31:04

该错误是 301 重定向,这意味着 Web 服务器将您重定向到末尾带有斜杠的 URL。

PHP 似乎会自动遵循此重定向,因此不会抛出错误,而 Python 不会。 您需要执行以下操作:

  1. 尝试连接到初始 URL
  2. 捕获任何 301 重定向,也可能捕获 302 重定向
  3. 如果存在重定向,则尝试连接到该 URL。

新的 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:

  1. Try to Connect to the initial URL
  2. Catch any 301 redirect and possibly 302 redirects as well
  3. If there was a redirect then try to connect to that URL instead.

The new URL should be available in the response headers.

HTH.

装纯掩盖桑 2024-08-03 22:31:04

[免责声明:这是我的答案的副本来自此处。 我知道有些人不喜欢这种复制,但这解释了为什么斜杠很重要。]

页面

http://mydomain.com/bla

假设您提供一个包含单击时的

<a href="more.html">Read more...</a>

,用户的浏览器将检索 http://mydomain.com/more .html。 如果您提供服务

http://mydomain.com/bla/

(使用相同的内容),浏览器将检索 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

http://mydomain.com/bla

that contains

<a href="more.html">Read more...</a>

On click, the user's browser would retrieve http://mydomain.com/more.html. Had you instead served

http://mydomain.com/bla/

(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.

小猫一只 2024-08-03 22:31:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文