如何设置HttpPost头在Java的Andr​​oid客户端请求 [英] How to Set HttpPost Headers for a Client Request in Java Android

查看:185
本文介绍了如何设置HttpPost头在Java的Andr​​oid客户端请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦了Apache HttpClient的正确发送HttpPost头。

I am having trouble getting the Apache HttpClient to correctly send an HttpPost Header.

我没有问题发送名称值对和诸如此类的东西,但每当我设置或添加一个POST头,该请求时消失。

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.

我已经试过这两个和的setHeader的addHeader,以及一次尝试两者。

I have tried both setHeader and addHeader, as well as trying both at once.

下面是我的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.

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

编辑:这是一个的AsyncTask内所有正在运行如果该事项。我不认为它既然有抛出否则NetworkOnMainThread例外,但我想这可能是值得一提。

This is all running inside of an AsyncTask if that matters. I don't think it does since there is a NetworkOnMainThread exception thrown otherwise but I thought it might be worth mentioning.

推荐答案

尝试连接的服务,告诉你的HTTP头并捕获(只是打印普通的HTML)的输出。至少你会知道,如果你的头被真丢在路上或者别的东西。

Try to connect the service that tells your HTTP headers and capture (just print plain HTML) the output. At least you will know if your headers are really lost on the way or maybe something else.

这篇关于如何设置HttpPost头在Java的Andr​​oid客户端请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆