zoukankan      html  css  js  c++  java
  • okhttp添加自定义cookie

     
     
      1 package cn.x.request;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 
      7 import okhttp3.Call;
      8 import okhttp3.Cookie;
      9 import okhttp3.CookieJar;
     10 import okhttp3.HttpUrl;
     11 import okhttp3.OkHttpClient;
     12 import okhttp3.Request;
     13 import okhttp3.RequestBody;
     14 import okhttp3.Response;
     15 
     16 public abstract class BaseRequest {
     17     String url;
     18     String requestMethod;
     19     OkHttpClient okHttpClient ;
     20     RequestBody body ;
     21     Request request;
     22     Call call;
     23     Response response;
     24     
     25     public BaseRequest() {
     26         final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
     27         this.okHttpClient = new OkHttpClient.Builder()
     28                 .cookieJar(new CookieJar() {
     29                     @Override
     30                     public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
     31                         cookieStore.put(httpUrl.host(), list);
     32                     }
     33 
     34                     @Override
     35                     public List<Cookie> loadForRequest(HttpUrl httpUrl) {
     36                         List<Cookie> cookies = cookieStore.get(httpUrl.host());
     37                         return cookies != null ? cookies : new ArrayList<Cookie>();
     38                     }
     39                 })
     40                 .build();
     41     }
     42     
     43     
     44     public abstract void prepareRequest() throws Throwable;
     45     public abstract Response excuteRequest() throws Throwable;
     46 
     47 
     48     public String getUrl() {
     49         return url;
     50     }
     51 
     52 
     53     public void setUrl(String url) {
     54         this.url = url;
     55     }
     56 
     57 
     58     public String getRequestMethod() {
     59         return requestMethod;
     60     }
     61 
     62 
     63     public void setRequestMethod(String requestMethod) {
     64         this.requestMethod = requestMethod;
     65     }
     66 
     67 
     68     public OkHttpClient getOkHttpClient() {
     69         return okHttpClient;
     70     }
     71 
     72 
     73     public void setOkHttpClient(OkHttpClient okHttpClient) {
     74         this.okHttpClient = okHttpClient;
     75     }
     76 
     77 
     78     public RequestBody getBody() {
     79         return body;
     80     }
     81 
     82 
     83     public void setBody(RequestBody body) {
     84         this.body = body;
     85     }
     86 
     87 
     88     public Request getRequest() {
     89         return request;
     90     }
     91 
     92 
     93     public void setRequest(Request request) {
     94         this.request = request;
     95     }
     96 
     97 
     98     public Call getCall() {
     99         return call;
    100     }
    101 
    102 
    103     public void setCall(Call call) {
    104         this.call = call;
    105     }
    106 
    107 
    108     public Response getResponse() {
    109         return response;
    110     }
    111 
    112 
    113     public void setResponse(Response response) {
    114         this.response = response;
    115     }
    116     
    117 }
    View Code
     1 package cn.x.request;
     2 
     3 import java.util.List;
     4 
     5 import cn.x.common.AppHeader;
     6 import okhttp3.Cookie;
     7 import okhttp3.HttpUrl;
     8 import okhttp3.MediaType;
     9 import okhttp3.Request;
    10 import okhttp3.RequestBody;
    11 import okhttp3.Response;
    12 
    13 public class JsonRequest extends BaseRequest {
    14     String jsonString;
    15     String accessToken;
    16     
    17     public JsonRequest(String url ,String jsonString) {
    18         super();
    19         this.url = url;
    20         this.jsonString = jsonString;
    21         
    22     }
    23 
    24     @Override
    25     public void prepareRequest() throws Throwable {
    26         MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    27         AppHeader appHeader = new AppHeader(jsonString);
    28         body = RequestBody.create(JSON, jsonString);
    29         request = new Request.Builder()
    30                 .url(url)
    31                 .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
    32                 .addHeader("SIGN",appHeader.getSign())
    33                 .addHeader("T",appHeader.getTimespan())
    34                 .post(body)
    35                 .build();
    36         if( null != accessToken) {
    37             List<Cookie> cookieList = okHttpClient.cookieJar().loadForRequest(HttpUrl.get(url));
    38             Cookie cookie = new Cookie.Builder().name("accessToken").value(accessToken).domain(HttpUrl.get(url).host()).build();
    39             cookieList.add(cookie);
    40             okHttpClient.cookieJar().saveFromResponse(HttpUrl.get(url), cookieList);
    41         }
    42     }
    43 
    44     @Override
    45     public Response excuteRequest() throws Throwable {
    46         System.out.println();
    47         if(request.header("Authorization") != null) {
    48             System.out.println("Authorization: " + request.header("Authorization"));
    49         }
    50         System.out.println("url:" + url);
    51         System.out.println("request:" + jsonString);
    52         call = okHttpClient.newCall(request);
    53         response = call.execute();
    54         System.out.println("response:" + response.toString());
    55         return response;
    56     }
    57 
    58     public String getJsonString() {
    59         return jsonString;
    60     }
    61 
    62     public void setJsonString(String jsonString) {
    63         this.jsonString = jsonString;
    64     }
    65 
    66     public String getAccessToken() {
    67         return accessToken;
    68     }
    69 
    70     public void setAccessToken(String accessToken) {
    71         this.accessToken = accessToken;
    72     }
    73     
    74     
    75     
    76     
    77 
    78 }
    View Code
  • 相关阅读:
    js学习笔记
    Bootstrap学习笔记
    css学习任务二:切图写代码
    九宫格改进
    js学习笔记
    XHTML复习笔记
    html基础知识复习笔记
    css学习任务一:绘制九宫格
    如何不使用第三个变量来交换两个数的值
    算术右移与逻辑右移
  • 原文地址:https://www.cnblogs.com/xiaodebing/p/9753602.html
Copyright © 2011-2022 走看看