如何在标题中传递令牌作为http get和http post?

时间:2018-01-08 09:53:47

标签: android json

我正在尝试实现一个简单的JSON get并发布到将标记作为标头发送的URL。有人可以帮助我将标题作为http get和http post传递给标题吗?

if (api.method.equals("GET")) {

 try {
                HttpGet httpget = new HttpGet(SERVER_URL + api.toString()+URL.replaceAll(" ","%20").trim());
                //httpget.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
                httpget.setHeader("Content-type", "application/json");
                httpget.setHeader("Authentication", "mytokenvalue");
                //List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                //nameValuePairs.add(new BasicNameValuePair("Content-type", "application/json"));
                //httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.d("111111111",SERVER_URL+api.toString()+URL.replaceAll(" ","%20").trim());
                HttpResponse response = httpclient.execute(httpget);
                return readStream(response.getEntity().getContent());
            } catch (HttpHostConnectException e) {
                e.printStackTrace();
            }
        }

1 个答案:

答案 0 :(得分:0)

    HttpClient client = new DefaultHttpClient();
  HttpPost request = new HttpPost(SERVER_URL
                     + api.toString()+URL.replaceAll(" ","%20").trim());
                request.setHeader(new BasicHeader("Content-Type","application/json"));
                request.setHeader(newBasicHeader("Authorization,"token=mytokenvalue"));

                response = client.execute(request);