【问题标题】:WebClient gets 401 when requesting a directory without a slashWebClient 在请求没有斜杠的目录时得到 401
【发布时间】:2014-08-13 14:26:09
【问题描述】:

我有一个使用 nginx 的 Web 服务器,配置了 HTTPS 和基本身份验证。

我正在尝试使用我的 WebClient 和 PowerShell 来查询它

$wc = new-object System.Net.WebClient
$wc.Credentials = Get-Credential
return $wc.DownloadString($url)

这适用于以下$urls

https://server.com
https://server.com/
https://server.com/directory/
https://server.com/page.php
https://server.com/directory/index.php

但是对于以下$urls,我得到The remote server returned an error: (401) Unauthorized.

https://server.com/directory
https://server.com/otherdirectory
https://server.com/directory/directory

起初我认为这是由于重定向,但考虑到一些工作示例,这没有意义。也许是我的 nginx 配置?

【问题讨论】:

  • “不起作用”是什么意思?
  • 它得到一个 401,根据标题。可能应该在我的问题正文中提到这一点 - 现在更新谢谢。
  • 您在使用普通浏览器导航时是否得到相同的行为?问题似乎很可能出在 Web 服务器/Web 应用程序的设置上。您可以在 nginx trailing slash 或类似网站上进行互联网搜索,以找到配置 nginx 以按照您想要的方式处理此问题的方法。我从使用这些关键字的快速搜索中发现了几个有希望的链接。祝你好运!
  • 不,它在浏览器中工作正常
  • 啊哈,但是浏览器重定向给一个斜线。我以前怎么没有注意到这一点

标签: https webclient basic-authentication http-status-code-301 http-status-code-401


【解决方案1】:

我目前认为这是WebClient 类中的一个错误。以下是我与服务器交互的摘要:

------------------------------------------------------------------------
GET /directory HTTP/1.1
Host: example.com
------------------------------------------------------------------------
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 27 Jun 2014 02:37:45 GMT
Content-Type: text/html
Content-Length: 203
Connection: keep-alive
WWW-Authenticate: Basic realm="sup"

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
------------------------------------------------------------------------
GET /directory HTTP/1.1
Authorization: Basic bmFjaHQ6aGVsbG8=
Host: example.com
------------------------------------------------------------------------
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 27 Jun 2014 02:37:46 GMT
Content-Type: text/html
Content-Length: 193
Location: http://example.com/directory/
Connection: keep-alive

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
------------------------------------------------------------------------
GET /directory/ HTTP/1.1
Host: example.com
------------------------------------------------------------------------
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 27 Jun 2014 02:37:47 GMT
Content-Type: text/html
Content-Length: 203
Connection: keep-alive
WWW-Authenticate: Basic realm="sup"

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
------------------------------------------------------------------------

此时WebClient抛出异常,说明服务器返回问题中所述的错误。

它可能应该在第三个请求中提供身份验证令牌,或者至少使用提供的身份验证令牌响应另一个请求来响应 401。

我希望其他人确认这一点(最好使用 .NET 4.5.2),所以我可以接受这个作为答案。

【讨论】:

    猜你喜欢
    • 2012-10-13
    • 2015-09-20
    • 2021-05-21
    • 1970-01-01
    • 2020-11-10
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多