0

我有一个 Java 的实现HttpServer,我用它来测试它的基本功能,这意味着我可以即时调整提供给客户端的内容。我的代码使用的是 apache http 客户端。

我想使用此实现测试一些身份验证,但我遇到了一些问题。我的问题是我的代码从不进行身份验证,发送初始请求并且服务器以 401 响应,但 http 客户端从不响应。它遍历身份验证类型列表,但从不选择 BASIC

如果我使用浏览器连接到相同的 url,我会收到提示,当我提交凭据时它会登录。如果我更改代码以便代码尝试登录到其他服务器,则它是成功的,所以我知道两端都有效!

我已经在客户端和服务器端连接了连接,我可以看到的差异是:

  • 当连接成功时,后续请求将作为 POST 而不是 GET 发送。
  • 当我的服务器响应时,身份验证标头是 Www 而不是 WWW(因为它工作时)

编辑:

查看 http 代码,区分大小写应该不会导致任何问题。第一个响应是失败的响应,第二个响应是有效的响应:

失败

GET /testing HTTP/1.1
Host: 192.168.30.65:8000
Connection: Keep-Alive
User-Agent: Apache-HttpAsyncClient/4.0-beta1 (java 1.5)

HTTP/1.1 401 Unauthorized
Content-length: 0
Www-authenticate: Basic realm="myRealm"

作品

GET /svn HTTP/1.1
Host: svnserver
Connection: Keep-Alive
User-Agent: Apache-HttpAsyncClient/4.0-beta1 (java 1.5)

HTTP/1.1 401 Authorization Required
Date: Mon, 16 Apr 2012 09:51:58 GMT
Server: Apache/2.2.3 (CentOS)
WWW-Authenticate: Basic realm="Subversion Repository"
Content-Length: 475
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at svnserver Port 80</address>
</body></html>

GET /svn HTTP/1.1
Host: svnserver
Connection: Keep-Alive
User-Agent: Apache-HttpAsyncClient/4.0-beta1 (java 1.5)
Authorization: Basic YQVkd2Gm3GS6dXNjbMk5
4

0 回答 0