简体   繁体   English

如何在Java Android中为客户端请求设置HttpPost标头

[英]How to Set HttpPost Headers for a Client Request in Java Android

I am having trouble getting the Apache HttpClient to correctly send an HttpPost Header. 我无法让Apache HttpClient正确发送HttpPost标头。

I have no problems sending name value pairs and whatnot, but whenever I set or add a POST Header, it disappears when the request is made. 我没有问题发送名称值对和诸如此类的东西,但每当我设置或添加POST标题时,它会在请求发生时消失。

I have tried both setHeader and addHeader, as well as trying both at once. 我已经尝试了setHeader和addHeader,以及同时尝试这两个。

Here is my code: 这是我的代码:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://posttestserver.com/post.php");
    httppost.setHeader("Authorization: Bearer", accessToken);
    httppost.addHeader("Authorization: Bearer", accessToken);
    Log.d("DEBUG", "HEADERS: " + httppost.getFirstHeader("Authorization: Bearer"));

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httppost, responseHandler);

    Log.d("DEBUG", "RESPONSE: " + responseBody);

Additionally, the debug statement before the request is executed prints out the correct header, so I know it is being added, then just dropped later. 此外,执行请求之前的调试语句打印出正确的标题,因此我知道它正在添加,然后稍后删除。

Any help would be much appreciated! 任何帮助将非常感激!

EDIT: This is all running inside of an AsyncTask if that matters. 编辑:如果重要的话,这都在AsyncTask中运行。 I don't think it does since there is a NetworkOnMainThread exception thrown otherwise but I thought it might be worth mentioning. 我认为没有,因为有一个NetworkOnMainThread异常抛出,但我认为值得一提。

At least one mistake in your code. 代码中至少有一个错误。 Try this instead: 试试这个:

httppost.setHeader("Authorization", "Bearer "+accessToken);

Try to connect the service that tells your HTTP headers and capture (just print plain HTML) the output. 尝试连接告诉HTTP标头的服务并捕获(只打印纯HTML)输出。 At least you will know if your headers are really lost on the way or maybe something else. 至少你会知道你的标题是真的丢失了还是别的东西。

You said the header is not on the response in one of your replies, just to clarify, headers don't come back on the response, they are just sent on the request to the server. 你说在你的一个回复中,标题不在响应中,只是为了澄清,标题不会回复到响应,它们只是在请求时发送到服务器。 A network trace or fiddler session can show the request and response, and this should give you a definitive answer of whether they are getting dropped or not. 网络跟踪或提琴手会话可以显示请求和响应,这应该给你一个明确的答案,他们是否会被丢弃。

try using java.net.URLConnection.addRequestProperty(String field, String newValue) 尝试使用java.net.URLConnection.addRequestProperty(String field, String newValue)

http://developer.android.com/reference/java/net/URLConnection.html#addRequestProperty%28java.lang.String,%20java.lang.String%29 http://developer.android.com/reference/java/net/URLConnection.html#addRequestProperty%28java.lang.String,%20java.lang.String%29

Personally i prefer use an other methos to set properly the http authorization: 我个人更喜欢使用其他方法来正确设置http授权:

httpClient.getCredentialsProvider().setCredentials(
  new AuthScope(hostname, port), 
  new UsernamePasswordCredentials(user, pass));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM