4

请注意,我已经在 SOAPUI 中测试了以下内容,并且使用相同的 URL 和授权凭据似乎可以按预期工作(即返回 200 状态)。它在 SOAPUI 中运行良好的事实使我认为在构建我的请求以处理它是 https 的事实时我应该做一些特定的事情。

尝试使用以下方法使用 DefaultHttpClient (v4.2.3) POST 到 URL:

String requestUrl = "https://test.host.name:9093/ws/simple/serviceName";
HttpPost httpPost = new HttpPost(requestUrl);

String username = "username@domainname";
String password = "pass-word-string";
String usernameAndPassword = username + ":" + password;
String encodedUserNameAndPassword = new sun.misc.BASE64Encoder().encode(usernameAndPassword.getBytes());
httpPost.setHeader("Authorization", encodedUserNameAndPassword);

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().toString());

HttpEntity entity = response.getEntity();           
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);

EntityUtils.consume(entity);

我遇到的问题是服务器响应 400 状态和错误请求消息:

[DEBUG] wire - << "HTTP/1.1 400 Bad Request[\r][\n]"
[DEBUG] wire - << "Connection: close[\r][\n]"
[DEBUG] wire - << "Server: Jetty(6.1.26-boomi)[\r][\n]"
[DEBUG] wire - << "[\r][\n]"

如果我删除身份验证部分,那么我会得到一个“401 Not Authorized”,这至少表明服务器正在等待我的请求:

[DEBUG] wire - << "HTTP/1.1 401 Unauthorized[\r][\n]"
[DEBUG] wire - << "WWW-Authenticate: Basic realm="BoomiBasicRealm"[\r][\n]"
[DEBUG] wire - << "Content-Type: text/html; charset=iso-8859-1[\r][\n]"
[DEBUG] wire - << "Cache-Control: must-revalidate,no-cache,no-store[\r][\n]"
[DEBUG] wire - << "Content-Length: 1396[\r][\n]"
[DEBUG] wire - << "Server: Jetty(6.1.26-boomi)[\r][\n]"

服务器管理员说他可以看到以下内容进入服务器日志:

SSL renegotiate denied: java.nio.channels.SocketChannel[connected local=/192.168.20.86:9093 remote=/ip.ip.ip.ip:61356]”

奇怪的是,我已经使用相同的代码成功发布到使用 https 的其他 URL,尽管没有在地址中使用不同的端口,并且它可以正常工作。

当我继续调查时,服务器管理员正在从他们的角度查看它,但它在 SOAPUI 中工作的事实让我认为我需要做一些不同的事情。

我注意到 SOAPUI 使用的是 Apache-HttpClient v4.1.1,而我使用的是 4.2.3,所以我切换到 4.1.1 并没有什么不同。因此,我猜测 SOAPUI 在我需要模仿的情况下正在做一些不同的事情。

是否有任何 SOAPUI 开发人员可以提供建议?

4

2 回答 2

0

The sun Base64 encoder will add newlines after a certain number of characters. if your encoded username/password is too long, you will be sending a broken authorization header. trying removing any whitespace from the encode authorization value. (as a side note, you should always use an explicit encoding with String.getBytes())

String encodedUserNameAndPassword = new sun.misc.BASE64Encoder()
    .encode(usernameAndPassword.getBytes("UTF-8"))
    .replaceAll("\\s+", "");

That said, i'm pretty sure apache http client can handle this for you. You should probably be following this example.

于 2013-02-26T16:30:03.430 回答
0

也许您应该启用HttpClient 的抢先授权,而不是手动添加Authorization标头。

另请参阅使用 Apache HttpClient 4 的抢先式基本身份验证

于 2013-02-26T16:04:31.647 回答