与Android的授权HTTP POST请求Android、HTTP、POST

由网友(上帝喜欢偏执狂)分享简介:当我把授权与SetHeader可以从HttpPost头,然后从主机请求消失,总有错误400(错误请求)返回。同code是工作正常的纯Java(无机器人),当我删除设置授权头还对Android的它工作正常,但我需要授权。这是一个code(域更改):When I set "Authorization" header w...

当我把授权与SetHeader可以从HttpPost头,然后从主机请求消失,总有错误400(错误请求)返回。同code是工作正常的纯Java(无机器人),当我删除设置授权头还对Android的它工作正常,但我需要授权。 这是一个code(域更改):

When I set "Authorization" header with setHeader from HttpPost then hostname disappears from request and there is always error 400 (bad request) returned. Same code is working fine on pure java (without android) and when I remove setting "Authorization" header also on android it works fine, but I need authorization. This is a code (domain changed):

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://myhost.com/test.php");
post.setHeader("Accept", "application/json");
post.setHeader("User-Agent", "Apache-HttpClient/4.1 (java 1.5)");
post.setHeader("Host", "myhost.com");
post.setHeader("Authorization",getB64Auth());
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data[body]", "test"));
AbstractHttpEntity ent=new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
ent.setContentEncoding("UTF-8");
post.setEntity(ent);
post.setURI(new URI("http://myhost.com/test.php"));
HttpResponse response =client.execute(post);

使用Base64这样的:密码登录恩codeD:

方法getB64Auth()返回。YnxpcYRlc3RwMTulHGhlSGs =,但它并不重要

Method getB64Auth() returns "login:password" encoded using Base64 like: "YnxpcYRlc3RwMTulHGhlSGs=" but it's not important.

这是一块的lighttpd的error.log中,当上述$ C $,C是纯Java调用:

This is a piece of lighttpd's error.log when above code is invoked on pure java:

2011-02-23 15:37:36: (request.c.304) fd: 8 request-len: 308
POST /test.php HTTP/1.1
Accept: application/json
User-Agent: Apache-HttpClient/4.1 (java 1.5)
Host: myhost.com
Authorization: Basic YnxpcYRlc3RwMTulHGhlSGs=
Content-Length: 21
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Encoding: UTF-8
Connection: Keep-Alive

HTTP/1.1 200 OK
Content-type: text/html
Transfer-Encoding: chunked

和从access.log里记录(IP改变):

and record from access.log (IP changed):

1.1.1.1 myhost.com - [23/Feb/2011:15:37:36 +0100] "POST /test.php HTTP/1.1" 200 32 "-" "Apache-HttpClient/4.1 (java 1.5)"

在同一$ C $,C是Android的调用,我得到这个在日志中:

When the same code is invoked on android, I get this in logs:

POST /test.php HTTP/1.1
Accept: application/json
User-Agent: Apache-HttpClient/4.1 (java 1.5)
Host: myhost.com
Authorization: Basic YnxpcYRlc3RwMTulHGhlSGs=

Content-Length: 21
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Encoding: UTF-8
Connection: Keep-Alive
Expect: 100-Continue


2011-02-23 15:45:10: (response.c.128) Response-Header:
HTTP/1.1 400 Bad Request
Content-Type: text/html
Content-Length: 349
Connection: close

access.log的:

access.log:

1.1.1.1 - - [23/Feb/2011:15:45:10 +0100] "POST /test.php HTTP/1.1" 400 349 "-" "Apache-HttpClient/4.1 (java 1.5)"

如何获得授权与邮政工作的机器人? 当我使用的HttpURLConnection而不是HttpClient的是没有什么区别。

How to get Authorization with POST working on android? When I use HttpURLConnection instead of HttpClient it is no difference.

推荐答案

感谢Samuh的提示:) 有插入额外的换行符有没有办法在GET请求,但是事项POST的。 这是正确的方式来产生的机器人(在getB64Auth在这种情况下)Authorization头:

Thanks to Samuh for a hint :) There was an extra newline character inserted which has no means in GET requests, but matters in POST ones. This is proper way to generate Authorization header in android (in getB64Auth in this case):

 private String getB64Auth (String login, String pass) {
   String source=login+":"+pass;
   String ret="Basic "+Base64.encodeToString(source.getBytes(),Base64.URL_SAFE|Base64.NO_WRAP);
   return ret;
 }

该Base64.NO_WRAP标志少了点。

The Base64.NO_WRAP flag was lacking.

阅读全文

相关推荐

最新文章