欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-10 20:25:10
本文实例讲述了android实现http中请求访问添加cookie的方法。分享给大家供大家参考,具体如下: 第一种 hashmap

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

第一种

hashmap<string, string> map = new hashmap<string, string>();
map.put("cookie","p1u_id=4eb591e73554db0f4d3300cb656113abfb968ef6b0ee2b5de0a35caa5217c51faa028b453576b35c");
networkconnection.setheaderlist(map);
networkconnection.connectionresult result = networkconnection.execute();

第二种是用okhttp

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程序设计有所帮助。