Java Android、HttpURLConnection、PUT和验证器

Java Android、HttpURLConnection、PUT和验证器,java,android,httpurlconnection,put,authenticator,Java,Android,Httpurlconnection,Put,Authenticator,我在前面已经将验证器设置为: url = new URL(UPLOAD_URL); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("PUT"); urlConnection.setRequestProperty("content-type", "application/json"); urlConnection.setFixedLengthStreamingMod

我在前面已经将验证器设置为:

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();
我提供了正确的登录详细信息,但服务器用
401
代码响应。(一个类似的GET请求也可以工作。)除此之外,在连接和写入流的过程中没有调用方法
getPasswordAuthentication()
。(我之所以知道这一点,是因为我输入了
Log.v(“app”,“password here”)


为什么会这样?

我无法回答为什么使用验证器不起作用,但我通常使用这种方法:

Authenticator.setDefault(new Authenticator()
{
 @Override
  protected PasswordAuthentication getPasswordAuthentication()
  {
    return new PasswordAuthentication(loginNameString, passwordString.toCharArray());
  }
});

试试看。使用基本身份验证就足够了。

PUT这只是一种请求类型。对于web请求,我通常使用基本身份验证。这对你不起作用吗?不,还没有。我们不知道问题出在哪里-我们会继续努力!你解决这个问题了吗。
    String webPage = "http://192.168.1.1";
    String name = "admin";
    String password = "admin";

    String authString = name + ":" + password;
    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
    String authStringEnc = new String(authEncBytes);

    URL url = new URL(webPage);
    URLConnection urlConnection = url.openConnection();
    urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
    InputStream is = urlConnection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);