使用我的应用程序连接到php .htaccess

时间:2019-07-06 08:11:21

标签: java android .htaccess http-post

我通过.htaccess文件锁定了php页面(用户名和密码)。 我尝试使用我的android应用程序连接到该php。

我在这里尝试此页面:Connect to php page using .htaccess with android application

但这不起作用。

我的代码:

HttpClient cnxHttp = new DefaultHttpClient();
HttpPost paramCnx = new HttpPost(strings[0]);
paramCnx.setHeader("Authorization", "Basic " +  new Base64().encodeToString("user:mdp".getBytes("UTF-8"), Base64.DEFAULT));

我在getBytes上收到错误消息:UnsupportedEncodingException。 我不明白为什么,somoene可以向我解释并提供正确的代码吗?

1 个答案:

答案 0 :(得分:0)

我找到了。 答案:

String authorizationString = "Basic " + Base64.encodeToString(
                    ("your_login" + ":" + "your_password").getBytes(),
                    Base64.NO_WRAP); //Base64.NO_WRAP flag
            post.setHeader("Authorization", authorizationString);