url = new URL(UPLOAD_URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("PUT");
urlConnection.setRequestProperty("content-type", "application/json");
urlConnection.setFixedLengthStreamingMode(responseJSONArray.toString(2).getBytes("UTF8").length);
urlConnection.setDoInput(false);
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(this.CONNECT_TIMEOUT);
urlConnection.connect();
OutputStream output = urlConnection.getOutputStream();
output.write(responseJSONArray.toString(2).getBytes("UTF8"));
output.close();
我之前也已经将 Authenticator 设置为:
Authenticator.setDefault(new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(loginNameString, passwordString.toCharArray());
}
});
我提供了正确的登录详细信息,但服务器以401
代码响应。(不过,类似的 GET 请求有效。)除此之外,getPasswordAuthentication()
在连接和写入流的过程中不会调用该方法。(我知道这一点是因为我输入了Log.v("app", "password here")
.)
这是为什么?