如何在httppost / httpget请求中设置标头

时间:2014-12-15 11:57:16

标签: android

我有像#34;这样的网络服务post -H'授权:令牌令牌=" 2hwhdh443hdhfh43j3jdej3j3"'。如何在httpPost / get请求中设置此标头。

我的代码在这里 -

HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(MainActivity.baseUrl
                    + "messages");
            request.setHeader(new BasicHeader("Content-Type","application/json"));
            request.setHeader(new BasicHeader("Authorization: Token","token= 2hwhdh443hdhfh43j3jdej3j3"));

            response = client.execute(request);

但这会给出HTML响应。请任何人指导我。

2 个答案:

答案 0 :(得分:9)

尝试删除“Content-Type”标题并将“授权”修改为 -

request.addHeader("Authorization","Token token=2hwhdh443hdhfh43j3jdej3j3");

为我工作。

答案 1 :(得分:0)

你做的只是愚蠢的错误。就这样做。

替换它: -

 request.setHeader(new BasicHeader("Authorization: Token","token= 2hwhdh443hdhfh43j3jdej3j3"));

对此: -

 request.setHeader(new BasicHeader("Authorization: Token","2hwhdh443hdhfh43j3jdej3j3"));

希望这会有所帮助。 :)