gpt4 book ai didi

android - 如何在android中使用摘要认证?

转载 作者:可可西里 更新时间:2023-11-01 19:05:46 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,我在其中通过服务器验证用户名/密码。最初服务器正在实现 Basic 身份验证,因此我的代码工作正常但现在服务器已更改为 Digest 身份验证,所以我的旧代码不起作用。

使用Digest 身份验证应该做哪些改变?

我的代码如下:

private boolean authenticateUser() 

{

try
{
String url_str = "http://serverweb.com/checkauthentication.php";

HttpPost post = new HttpPost(url_str);

Log.v("AUTHENTICATION URL = ", url_str);
post.addHeader("Authorization","Basic "+getCredentials());
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String response_body = client.execute(post, responseHandler);

Log.v("SERVER RESPONSE DATA = ", response_body);

XMLDataParser.parseXML(XMLDataParser.USER_INFORMATION_PARSER_CODE, response_body);

List<Cookie> cookies = client.getCookieStore().getCookies();
if (!cookies.isEmpty())
{
for (int i = 0; i < cookies.size(); i++)
{
XMLData.cookie = cookies.get(i);
}
}

return true;
}
catch (MalformedURLException mue)
{
Log.i("MalformedURLException", " "+mue.getMessage());
displayDialog("User Does Not exist");
return false;
}
catch (IOException ioe)
{
Log.i("IOException", " "+ioe.getMessage());
displayDialog("User Does Not exist");
return false;
}
catch (Exception e)
{
Log.i("Exception", " "+e.getMessage());
displayDialog("Error");
return false;
}
}
private String getCredentials()
{
String u=edit_username.getText().toString();
String p=edit_password.getText().toString();

Log.v("USER NAME = ",u);
Log.v("PASSWORD = ",p);
return(Base64.encodeBytes((u+":"+p).getBytes()));
}

最佳答案

您需要创建一个具有所需凭据的 HttpHostHttpContext 对象,并将其提供给 execute 方法。

这是一个示例代码,您的身份验证独立于后端身份验证。 android 的 http 客户端将负责将其转换为适当的格式。检查此示例代码,这仅供您引用,不能直接在您的代码中使用。 :)

此代码在您的 Activity 中:

@Override
public void onResume(){
super.onResume();
AsyncTask<String, Void, Void> httpTask = new TestHttpThread();
httpTask.execute("test_url","test_user","test_password");
}

AsyncActivity 示例:

private class TestHttpThread extends AsyncTask<String, Void, Void>{

@Override
protected Void doInBackground(String... params) {
if(params.length > 0){
String url = params[0];
String username = params[1];
String password = params[2];

try {
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("test user agent");

URL urlObj = new URL(url);
HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort());
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);

CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(scope, creds);
HttpContext credContext = new BasicHttpContext();
credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);

HttpGet job = new HttpGet(url);
HttpResponse response = httpClient.execute(host,job,credContext);
StatusLine status = response.getStatusLine();
Log.d(TestActivity.TEST_TAG, status.toString());
httpClient.close();
}
catch(Exception e){
e.printStackTrace();
}

}
return null;
}
}

关于android - 如何在android中使用摘要认证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704607/

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