gpt4 book ai didi

android - UrlConnection 使用 Java 代码但不适用于 Android - 在 Digest Authenticated Server 中使用

转载 作者:行者123 更新时间:2023-11-29 02:16:45 24 4
gpt4 key购买 nike

我正在使用 URLConnection 连接到主服务器。服务器实现摘要认证。如果我使用 java 库连接到服务器,则连接成功。但是,如果我对 android 使用相同的代码,连接将被拒绝,原因是 - 用户名和密码不匹配。

这是我的 Java 项目的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

public class Connect {

static StringBuilder sb;

public static void main(String args[]) {

String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";

Authenticator.setDefault(new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
return pa;
}
});

BufferedReader in = null;
StringBuffer sb = new StringBuffer();

try {
URL url = new URL(strURL);

URLConnection connection = url.openConnection();

in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}

} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");

e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}

System.out.println("The Data is: " + sb.toString());
}
}

上面的代码工作正常,我能够连接到我的主机服务器,它正在实现摘要式身份验证。我无法使用与 Android 相同的代码进行连接。这是我的安卓代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Connect extends Activity {

static StringBuilder sb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";

Authenticator.setDefault(new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
// System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
return pa;
}
});

BufferedReader in = null;
StringBuffer sb = new StringBuffer();

try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();

in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}

} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}

Log.d("DATA", sb.toString());
}
}

如果相同的代码适用于 Java,那么它也应该适用于 Android。

代码在 Authenticator 中循环,因为它发现用户名和密码由于某种原因在 Android 代码中不匹配,而实际上是正确的。该代码非常适合 Java 项目。

最佳答案

while ((line = in.readLine()) != null) {  
sb.append(line);
}

去掉这段代码,只写

line = in.readLine().toString();
sb.append(line);

关于android - UrlConnection 使用 Java 代码但不适用于 Android - 在 Digest Authenticated Server 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3197814/

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