android实现http中请求访问添加cookie的方法

时间:2022-07-01 08:31:48

本文实例讲述了android实现http中请求访问添加cookie的方法。分享给大家供大家参考,具体如下:

第一种

?
1
2
3
4
HashMap<String, String> map = new HashMap<String, String>();
map.put("cookie","p1u_id=4eb591e73554db0f4d3300cb656113abfb968ef6b0ee2b5de0a35caa5217c51faa028b453576b35c");
networkConnection.setHeaderList(map);
NetworkConnection.ConnectionResult result = networkConnection.execute();

第二种是用okhttp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
OkHttpClient client = new OkHttpClient();
String returnData=null;
com.squareup.okhttp.Request request = new com.squareup.okhttp.Request.Builder()
  .addHeader("cookie", "p1u_id=4eb591e73554db0f4d3300cb656113abfb968ef6b0ee2b5de0a35caa5217c51faa028b453576b35c")
  .url(url)
  .build();
Response response = null;
try {
  response = client.newCall(request).execute();
  returnData=response.body().string();
  Log.i("ss", "url is " + url + "_______RETUNRN DATA IS " + returnData);
} catch (IOException e) {
  e.printStackTrace();
}

希望本文所述对大家Android程序设计有所帮助。