开发者

"401 Unauthorized" error while doing http post (cloned from curl command) in Android

i know that i'm not first, but i did search and didn't found solution. First of all i need to make this:

curl -user1:pass1 - X POST -d"data" http://www.mysite.com/pushes.xml

But i don't want to use curl lib for such easy request, beacuse i think that solution must be easy and painless. I've found this http login example, and tried to use it with

开发者_JAVA百科    nvps.add(new BasicNameValuePair("username", "user1"));
    nvps.add(new BasicNameValuePair("password", "pass1"));

But i think that tags "username" and "password" are wrong... and i just unable to get them.. so I ask you about help. Please help me to understand what i need to do to get thrrough this login. Thanks will be guaranteed :)

P.S. in the response i get msg "HTTP Basic: Access denied". But curl command works fine...


Check the API you are using. I had the similar problem until I checked it and it was "user", not "username".
So change nvps.add(new BasicNameValuePair("username", "user1"));
to nvps.add(new BasicNameValuePair("user", "user1")); and give it a try.


if server returns 401 you need to pass credential via special authenticator or header parameters:

    httpPost.addHeader(RestHelper.AUTHORIZATION_HEADER, RestHelper.AUTHORIZATION_HEADER_VALUE);

where 

    AUTHORIZATION_HEADER_VALUE = "Basic " + Base64Encoder.encode ("user:password");
    AUTHORIZATION_HEADER = "Authorization";

or

    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope("server.com", 443, AuthScope.ANY_SCHEME),
            new UsernamePasswordCredentials(LOGIN_VALUE, PASSWORD));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜