Java HttpServer基本身份验证问题

时间:2012-04-16 07:40:52

标签: java com.sun.net.httpserver apache-commons-httpclient

我有一个Java HttpServer的实现,我用它来测试它的基本功能,这意味着我可以动态调整为客户端提供的服务。我的代码使用的是apache http client。

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

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

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

  • 当连接成功时,后续请求将作为POST而不是GET发送。
  • 当我的服务器响应认证头时,Www not 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

0 个答案:

没有答案