首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android上带授权的HTTP POST请求

android上带授权的HTTP POST请求
EN

Stack Overflow用户
提问于 2011-02-23 22:58:44
回答 2查看 51.3K关注 0票数 16

当我用来自HttpPost的setHeader设置"Authorization“头时,主机名从请求中消失,并且总是返回错误400 (错误请求)。同样的代码在纯java (没有android)上工作得很好,当我在android上删除设置" authorization“头时,它工作得很好,但我需要授权。这是一个代码(域已更改):

代码语言:javascript
复制
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);

方法getB64Auth()返回使用Base64编码的"login:password“,例如:"YnxpcYRlc3RwMTulHGhlSGs=”,但这并不重要。

这是在纯java上调用上述代码时lighttpd的一段error.log:

代码语言:javascript
复制
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已更改):

代码语言:javascript
复制
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)"

当在android上调用相同的代码时,我在日志中得到如下内容:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)"

如何在android上使用POST获得授权?当我使用HttpURLConnection而不是HttpClient时,这是没有区别的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-24 02:18:47

感谢Samuh的提示:)插入了一个额外的换行符,它在GET请求中没有意义,但在POST请求中很重要。这是在android中生成Authorization头部的正确方式(本例中为getB64Auth ):

代码语言:javascript
复制
 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标志。

票数 57
EN

Stack Overflow用户

发布于 2013-11-10 17:37:38

简单地使用这个:

代码语言:javascript
复制
String authorizationString = "Basic " + Base64.encodeToString(
                        ("your_login" + ":" + "your_password").getBytes(),
                        Base64.NO_WRAP); //Base64.NO_WRAP flag
                post.setHeader("Authorization", authorizationString);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5092561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档