gpt4 book ai didi

java - Android 中的 HttpURLConnection header 最大长度

转载 作者:行者123 更新时间:2023-12-01 06:25:22 24 4
gpt4 key购买 nike

我有以下代码

更新:该问题仅存在于 Android 应用程序中,在 Java 应用程序中工作正常

Java(不适用于大 header 值)

URL url = new URL("MyUrl"); //<===This could even be http://www.google.com  
HttpURLConnection request = (HttpURLConnection) url.openConnection();

String userCredentials = "username:AAAAABBBBBBBBCCCCSSSSSSSSSDDDDDDDDDDBE4B97CC9E81DB";
String basicAuth = "Basic " + android.util.Base64.encodeToString(
userCredentials.getBytes(), android.util.Base64.DEFAULT);
request.setRequestProperty ("Authorization", basicAuth);

request.connect();
InputStream stream = (InputStream) request.getContent();

调用 getContent() 时出现 ClassNotFoundException

java.lang.ClassNotFoundException: Didn't find class "org.apache.harmony.awt.www.content.text.html__charset_us_ascii" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

在ClassLoader.class第514行

 protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
Class<?> clazz = findLoadedClass(className);


if (clazz == null) {
ClassNotFoundException suppressed = null;
try {
clazz = parent.loadClass(className, false);
} catch (ClassNotFoundException e) {
suppressed = e;
}

if (clazz == null) {
try {
clazz = findClass(className);
} catch (ClassNotFoundException e) {
e.addSuppressed(suppressed);
//className=org.apache.harmony.awt.www.content.text.html__charset_us_ascii
throw e; //<===The code crashes here

}
}
}

return clazz;
}

问题在于授权 header 的大小,即使它不是那么大(88 个字符)
如果我将 header 的值更改为较小的值,它就会起作用。
我可以以某种方式更改 header 的最大大小吗?

我已经在其他平台(Windows、Windows Phone、Web)上尝试过,它工作正常,问题不在我的服务器上。

jQuery(始终有效)

var usernameAndPasword=["username","AAAAABBBBBBBBCCCCSSSSSSSSSDDDDDDDDDDBE4B97CC9E81DB"].join(":");
var authorizationHeaderValue="Basic "+ window.btoa(usernameAndPasword);

$.ajax({
url : "http://...",
type: "GET",
beforeSend: function (request) { request.setRequestHeader("Authorization", authorizationHeaderValue); },
success: function(data, textStatus, jqXHR) { alert(data + "\n" + textStatus ); },
error: function (jqXHR, textStatus, errorThrown){ alert(jqXHR.responseText + " \n " + textStatus + " \n"+errorThrown); }
});

C#(始终有效)

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUristring);
request.Method = "GET";
request.Headers["Accept-Encoding"] = "gzip, deflate";

var bytes = Encoding.UTF8.GetBytes("username" + ":" + "AAAAABBBBBBBBCCCCSSSSSSSSSDDDDDDDDDDBE4B97CC9E81DB" );
var base64 = Convert.ToBase64String(bytes);
request.Headers["Authorization"] = "Basic " + base64;

HttpWebResponse webResponse = (HttpWebResponse)request.GetResponseAsync().Result;

最佳答案

当您使用 Base64 编码时,编码字符串的最大长度应为 64,如果使用 MIME,则编码字符串的最大长度应为 76。

更多信息请查看this

关于java - Android 中的 HttpURLConnection header 最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30188733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com