0

We are having issue with HttpURLConnection + Basic authentication when we have lengthy passwords.

Here is the sampple code:

URL url = new URL("http://localhost/data.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod(method);

String authString = clientId + ":" + clientSecret;
BASE64Encoder encoder = new BASE64Encoder();


String authHeader = encoder.encode(authString.getBytes()); 
conn.setRequestProperty("Authorization", authHeader);
conn.setRequestProperty("Accept", "application/json");

We tried to use

     String authString = clientId + ":" + clientSecret;
authString = "" + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());

But no luck with this.

HttpURLConnection with Basic Auth with passswords of certain length working fine but with lengthy passwords we have issue. And the error code returned is 400

Note: This is related to Login with Paypal integration https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/#making-first-call

and the sample code we have (provided by PayPal) also not working.

thank you in advance.

4

3 回答 3

1

我面临的问题是由于冗长的用户名、密码。

如果我使用下面的代码,它工作正常

    byte[] encodedBytes = org.apache.commons.codec.binary.Base64.encodeBase64(passwordString.getBytes("UTF-8"));
    String encodedString = new String(encoded, "UTF-8");

headers.put("授权", "基本" + 编码字符串);

于 2013-07-22T06:26:14.767 回答
0

您应该检查成功的身份验证,即。使用具有请求检查器(即 chrome)或仅使用wireshark 的网络浏览器。

这样你就可以看到向服务器发送了什么样的请求消息。

从这一点开始,您可以检查您的代码是否提供了相同类型的请求,如果它们不同,您至少可以找出问题所在。

尽管即使基本身份验证长度没有规范强制限制,但仍然存在一些依赖于平台的限制,如下所述:是否有实际的 HTTP 标头长度限制?

希望你觉得它有用。

于 2013-07-10T16:20:35.783 回答
0

尝试发布。

如果您使用本地服务器对此进行测试,请确保它对最大 URL 大小、最大参数长度、最大 POST 标头大小等没有任何限制。检查服务器端日志。

这可能不是问题,但此语句“authString.getBytes()”中可能存在错误。您正在根据系统默认编码将字符串转换为字节,这取决于 JVM/OS/Locale——您应该将显式编码传递给 getBytes()。

于 2013-07-10T16:06:37.350 回答