我正在编写一个连接到密码保护的 cPanel 服务器(Apache 2.2.22)页面的 Android 应用程序。当身份验证凭据正确时,我可以轻松连接。然而,当凭据不正确时,我的 Android 应用程序似乎会在 HttpURLConnection.getResponseCode() 方法中冻结。服务器日志显示从我的 Android 设备发送了数百个请求,所有请求都返回预期的 401,但出于某种原因,这在我的应用程序中没有反映出来。
以下是我的代码,在 AsyncTask 中执行:
最后,这里是一小部分的logcat日志:
您可以看到,该方法似乎只在随机时间后返回null。我等待的最长时间正好是15分钟。在最后两个信息日志之间,dalikvm也有几个内存日志(GC_CONCURRENT),但我已省略。
我还应该说,目前我没有使用https,尽管我不认为这会造成任何问题。无论是完整的答案还是只是告诉我哪些不是问题的评论,我都非常感谢您的反馈,因为我仍然不确定这个问题是服务器端还是客户端的。
非常感谢, William
编辑:我忘了提前提到,我正在使用自己的定制java.net.Authenticator附加我的身份验证凭据。
以下是我的代码,在 AsyncTask 中执行:
@Override
protected Integer doInBackground(String... bookInfoString) {
// Stop if cancelled
if(isCancelled()){
return null;
}
Log.i(getClass().getName(), "SendToDatabase.doInBackground()");
String apiUrlString = getResources().getString(R.string.url_vages_library);
try{
NetworkConnection connection = new NetworkConnection(apiUrlString);
connection.appendPostData(bookInfoString[0]);
int responseCode = connection.getResponseCode();
Log.d(getClass().getName(), "responseCode: " + responseCode);
return responseCode;
} catch(IOException e) {
return null;
}
}
这段代码使用了我自己的类NetworkConnection
,它只是一个围绕HttpURLConnection的基本包装器类,以避免重复编写代码。以下是代码:
public class NetworkConnection {
private String url;
private HttpURLConnection connection;
public NetworkConnection(String urlString) throws IOException{
Log.i(getClass().getName(), "Building NetworkConnection for the URL \"" + urlString + "\"");
url = urlString;
// Build Connection.
try{
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(1000 /* 1 seconds */);
connection.setConnectTimeout(1000 /* 1 seconds */);
} catch (MalformedURLException e) {
// Impossible: The only two URLs used in the app are taken from string resources.
e.printStackTrace();
} catch (ProtocolException e) {
// Impossible: "GET" is a perfectly valid request method.
e.printStackTrace();
}
}
public void appendPostData(String postData) {
try{
Log.d(getClass().getName(), "appendPostData() called.\n" + postData);
Log.d(getClass().getName(), "connection.getConnectTimeout(): " + connection.getConnectTimeout());
Log.d(getClass().getName(), "connection.getReadTimeout(): " + connection.getReadTimeout());
// Modify connection settings.
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
// Get OutputStream and attach POST data.
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(postData);
if(writer != null){
writer.flush();
writer.close();
}
} catch (SocketTimeoutException e) {
Log.w(getClass().getName(), "Connection timed out.");
} catch (ProtocolException e) {
// Impossible: "POST" is a perfectly valid request method.
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// Impossible: "UTF-8" is a perfectly valid encoding.
e.printStackTrace();
} catch (IOException e) {
// Pretty sure this is impossible but not 100%.
e.printStackTrace();
}
}
public int getResponseCode() throws IOException{
Log.i(getClass().getName(), "getResponseCode()");
int responseCode = connection.getResponseCode();
Log.i(getClass().getName(), "responseCode: " + responseCode);
return responseCode;
}
public void disconnect(){
Log.i(getClass().getName(), "disconnect()");
connection.disconnect();
}
}
最后,这里是一小部分的logcat日志:
05-03 11:01:16.315: D/vages.library.NetworkConnection(3408): connection.getConnectTimeout(): 1000
05-03 11:01:16.315: D/vages.library.NetworkConnection(3408): connection.getReadTimeout(): 1000
05-03 11:01:16.585: I/vages.library.NetworkConnection(3408): getResponseCode()
05-03 11:04:06.395: I/vages.library.MainActivity$SendToDatabase(3408): SendToDatabase.onPostExecute(null)
您可以看到,该方法似乎只在随机时间后返回null。我等待的最长时间正好是15分钟。在最后两个信息日志之间,dalikvm也有几个内存日志(GC_CONCURRENT),但我已省略。
我还应该说,目前我没有使用https,尽管我不认为这会造成任何问题。无论是完整的答案还是只是告诉我哪些不是问题的评论,我都非常感谢您的反馈,因为我仍然不确定这个问题是服务器端还是客户端的。
非常感谢, William
编辑:我忘了提前提到,我正在使用自己的定制java.net.Authenticator附加我的身份验证凭据。
public class CustomAuthenticator extends Authenticator {
Context mContext;
public CustomAuthenticator(Context context){
super();
mContext = context;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
String username = sharedPreferences.getString(SettingsActivity.KEY_USERNAME_PREFERENCE, null);
String password = sharedPreferences.getString(SettingsActivity.KEY_PASSWORD_PREFERENCE, null);
return new PasswordAuthentication(username, password.toCharArray());
}
}
我在活动的onCreate()
方法中设置了:
Authenticator.setDefault(new CustomAuthenticator(mContext));
另外,我使用curl请求密码保护资源,正如预期的那样收到了401错误。现在我假设问题出在客户端。
curl -v "http://yoururl.com" -u user:password
。如果它不起作用,问题可能出在服务器端。 - Esparver