Android - 具有基本身份验证的 HttpClient JSON POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12443050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - HttpClient JSON POST with Basic Authentication
提问by user1674948
I am trying to implement a simple JSONpost to the URL that accepts JSONin body with basic authentication on ANDROID.
我正在尝试对JSONURL实现一个简单的帖子,该 URLJSON在正文中接受基本身份验证ANDROID。
I have tried with HttpUrlConnectionbut I get "unauthorized"and my data gets send to the server.
我试过,HttpUrlConnection但我得到了"unauthorized",我的数据被发送到服务器。
Another way I tried is using HttpClient, but now I get a different problem. Authentication works perfect but the data is not sent to the server...
我尝试的另一种方法是使用HttpClient,但现在我遇到了不同的问题。身份验证工作完美,但数据没有发送到服务器......
Just to be sure, I setup a small test in a simple Java project (not Android Environment).
可以肯定的是,我在一个简单的 Java 项目(不是 Android 环境)中设置了一个小测试。
This is the code that I am using to POSTto the server:
这是我POST用于服务器的代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://localhost/api/v1/purchase/");
postMethod.setEntity(new StringEntity("{\"amount_adult\" : 1, \"object_id\" : 13}"));
postMethod.setHeader( "Content-Type", "application/json");
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);
String response = httpClient.execute(postMethod,resonseHandler);
System.out.println("response :" + response);
The code in the Java project works perfect.
Java 项目中的代码运行良好。
When I try exact same code in Android, I get internal server errorfrom the server, which means that JSON data has not been received.
当我在 Android 中尝试完全相同的代码时,我internal server error从服务器获取,这意味着尚未收到 JSON 数据。
I really don't understand why this is working in JAVA but not in Android.
我真的不明白为什么这在 JAVA 中有效,但在 Android 中无效。
The effect that I would like to achieve, is the following command:
我想达到的效果是以下命令:
curl --dump-header - -H "Content-Type: application/json" -X
POST --data '{"amount_adult":1, "object_id":13}'
--user travelbuddy:travelbuddy http://localhost/api/v1/purchase/
回答by Dmitry T.
String authorizationString = "Basic " + Base64.encodeToString( ("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT);...
String authorizationString = "Basic " + Base64.encodeToString( ("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT);...
The solution is a bit simpler actually:
解决方案实际上有点简单:
String authorizationString = "Basic " + Base64.encodeToString(
("travelbuddy" + ":" + "travelbuddy").getBytes(),
Base64.NO_WRAP); // <=== Do not add '\n'
回答by user1674948
Finally, everything is sorted. Anyone that might experience this problem following is the solution.
终于,一切都整理好了。任何可能遇到此问题的人都是解决方案。
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);
This Base64.encodeToString appends an extra "\n" to the end of the authorization string. Then on the server next line after "\n" character is treated as body of the request and of course incorrect.
此 Base64.encodeToString 将额外的“\n”附加到授权字符串的末尾。然后在服务器上“\n”字符之后的下一行被视为请求的主体,当然不正确。
By replacing all new lines in authorization string will solve the issue:
通过替换授权字符串中的所有新行将解决问题:
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
authorizationString.replace("\n", "");
postMethod.setHeader("Authorization", authorizationString);
Easy fix but hard to find ;-)
容易修复但很难找到;-)
回答by Till
I think the simplest way is:
我认为最简单的方法是:
postMethod.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(user, pass), "UTF-8", false));
回答by secondflying
I think you shouldn't use localhost in url
我认为你不应该在 url 中使用 localhost

