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

相关文章:

  • 2021-08-09
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2021-06-26
  • 2021-04-21
  • 2021-07-03
  • 2021-06-24
相关资源
相似解决方案