基本身份验证来访问的Andr​​oid assembla的REST API [英] Basic authentication to access assembla rest apis from android

查看:96
本文介绍了基本身份验证来访问的Andr​​oid assembla的REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用assembla的API从Android的环境,为我的项目。 我试图做的基本身份验证如下:

I want to use assembla apis from android environment for my project. I am trying to do basic authentication as follow :

String authentication = "username:password";
String encoding = Base64.encodeToString(authentication.getBytes(), 0);    

     URL url = new URL("https://www.assembla.com/");

        conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");

        conn.setRequestProperty("Authorization", "Basic " + encoding);
        conn.setDoOutput(true);

        conn.connect();
        System.out.println(conn.getResponseCode());

        System.out.println(conn.getResponseMessage());

我收到400错误的请求的输出。 是有什么问题,我现在用或其他一些事情是怎么了?网址

I am getting 400 and Bad Request in output. is there something wrong with URL that i am using or some other thing is going wrong?

推荐答案

它看起来像问题得到回答<一href="http://stackoverflow.com/questions/3677293/problem-with-basic-access-authentication-in-file-downloader">here.您需要使用Base64.NO_WRAP标志编码时,用户名,密码对:

It looks like the question was answered here. You need to use Base64.NO_WRAP flag when encoding username-password pair:

String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);

默认情况下,Android的Base64的UTIL增加了一个换行符的EN codeD字符串的结尾。这种无效的HTTP头和导致坏请求。

By default the Android Base64 util adds a newline character to the end of the encoded string. This invalidates the HTTP headers and causes the "Bad request".

在Base64.NO_WRAP标志告诉UTIL创建EN codeD字符串没有这样的换行符保持HTTP标头完好无损。

The Base64.NO_WRAP flag tells the util to create the encoded string without the newline character thus keeping the HTTP headers intact.

这篇关于基本身份验证来访问的Andr​​oid assembla的REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆