美文网首页
使用Volley获取多个cookie

使用Volley获取多个cookie

作者: yancy2430 | 来源:发表于2017-03-14 23:50 被阅读145次

    最近做一个移植APP需要编写登录功能,因为是加密源码没有办法直接写API,就打算通过WAP页面登录获取cookie的方式来验证登录。但因为使用的Volley进行网络请求。默认Volley只读取一个cookie,而这个项目使用了多个cookie,所以Volley无法满足我的需求。只好自己修改一下volley的代码。
    先看一下源码
    在Vollay里获取请求信息都在HurlStack(sdk_int>9会走这里)里面的performRequest,默认解析header方式如下

    Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
    response.addHeader(h);
    

    所以第一步先继承HurlStack

    1. 重写或者是继承一个HurlStack,在方法performRequest()中把cookie添加到response。
    2. 定义一个model包含请求返回的内容和cookie list。
    3. 重写一个返回对象是上面我们model的request,在request的parseNetworkResponse方法中,把获取到的cookie放到model里面。

    在我们的HurlStack中performRequest返回的是一个HttpResponse(header是一个list),我们的cookie也是添加到了它的headers里面。到目前还没有问题。但是此处返回的response还要经过一个转化,因为Request类的protected abstract Response<T> parseNetworkResponse(NetworkResponse paramNetworkResponse);参数为一个NetworkResponse,这个的headers是一个Map<String, String>,所以这就要求我们的多个cookie的key不能都为Set-Cookie。因为上面的代码中cookie的header.getKey()一般是Set-Cookie。

    下面是部分源码:
    HurlStack.class

     public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError
      {
    
        ...
        BasicHttpResponse response = new BasicHttpResponse(responseStatus);
        response.setEntity(entityFromConnection(connection));
        for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
          if (header.getKey() != null)
          {
            Header h = new BasicHeader((String)header.getKey(), (String)((List)header.getValue()).get(0));
            response.addHeader(h);
          }
        }
        return response;
      }
    

    BasicNetwork.class

      private static Map<String, String> convertHeaders(Header[] headers)
      {
        Map<String, String> result = new HashMap();
        for (int i = 0; i < headers.length; i++) {
          result.put(headers[i].getName(), headers[i].getValue());
        }
        return result;
      }
    

    解决办法

    自定义MyHurlStack.class

    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
          throws IOException, AuthFailureError {
        String url = request.getUrl();
        HashMap map = new HashMap();
        map.putAll(request.getHeaders());
        map.putAll(additionalHeaders);
        if (this.mUrlRewriter != null) {
          String parsedUrl = this.mUrlRewriter.rewriteUrl(url);
          if (parsedUrl == null) {
            throw new IOException("URL blocked by rewriter: " + url);
          }
    
          url = parsedUrl;
        }
    
        URL parsedUrl1 = new URL(url);
        HttpURLConnection connection = this.openConnection(parsedUrl1, request);
        Iterator responseCode = map.keySet().iterator();
    
        while (responseCode.hasNext()) {
          String protocolVersion = (String) responseCode.next();
          connection.addRequestProperty(protocolVersion, (String) map.get(protocolVersion));
        }
    
        setConnectionParametersForRequest(connection, request);
        ProtocolVersion protocolVersion1 = new ProtocolVersion("HTTP", 1, 1);
        int responseCode1 = connection.getResponseCode();
        if (responseCode1 == -1) {
          throw new IOException("Could not retrieve response code from HttpUrlConnection.");
        } else {
          BasicStatusLine responseStatus =
              new BasicStatusLine(protocolVersion1, connection.getResponseCode(),
                  connection.getResponseMessage());
          BasicHttpResponse response = new BasicHttpResponse(responseStatus);
          response.setEntity(entityFromConnection(connection));
    
          for (Object o : connection.getHeaderFields().entrySet()) {
            Entry header = (Entry) o;
            if (header.getKey() != null) {
            // 如果是cookie则保存为name = Set-Cookie + 序号,保证key不同
              if (header.getKey().equals("Set-Cookie")) {
                List headerList = (List) header.getValue();
                for (int i = 0; i < headerList.size(); i++) {
                  BasicHeader h =
                      new BasicHeader((String) header.getKey() + i, (String) headerList.get(i));
                  response.addHeader(h);
                }
              } else {
                List headerList = (List) header.getValue();
                for (Object aHeaderList : headerList) {
                  BasicHeader h = new BasicHeader((String) header.getKey(), (String) aHeaderList);
                  response.addHeader(h);
                }
              }
            }
          }
    
          return response;
        }
      }
    

    MyRequest.class

    //使用正则表达式从reponse的头中提取cookie内容的子串
      private static final Pattern pattern = Pattern.compile("Set-Cookie\\d*");
    
    
      @Override
      protected Response<MyResponse> parseNetworkResponse(NetworkResponse networkResponse) {
        try {
          MyResponse response = new MyResponse();
          String jsonString =
              new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
    
          response.setJsonString(jsonString);
          // get all cookie
          List<Cookie> cookies = new ArrayList<>();
          for (String key : networkResponse.headers.keySet()) {
            Matcher matcher = pattern.matcher(key);
            if (matcher.find()) {
              cookieFromResponse = networkResponse.headers.get(key);
              cookieFromResponse = cookieFromResponse.substring(0, cookieFromResponse.indexOf(";"));
              String keyValue[] = cookieFromResponse.split("=", -1);
              Cookie cookie = new BasicClientCookie(keyValue[0], keyValue[1]);
              cookies.add(cookie);
            }
          }
    
          response.setCookies(cookies);
    
          return Response.success(response, HttpHeaderParser.parseCacheHeaders(networkResponse));
        } catch (UnsupportedEncodingException e) {
          return Response.error(new ParseError(e));
        }
      }
    

    MyResponse.class

    public class MyResponse{
    
      private static String TAG = "MyResponse";
    
      private int status = 0;
    
      private String jsonString = "";
    
      private List<Cookie> cookies = new ArrayList<>();
    
      public int getStatus() {
        return status;
      }
    
      public void setStatus(int status) {
        this.status = status;
      }
    
      public String getJsonString() {
        return jsonString;
      }
    
      public void setJsonString(String jsonString) {
        this.jsonString = jsonString;
      }
    
      public List<Cookie> getCookies() {
        return cookies;
      }
    
      public void setCookies(List<Cookie> cookies) {
        this.cookies = cookies;
      }
    }
    
    

    VolleyHelper.class

    public static void init(Context context) {
        queue = Volley.newRequestQueue(context, new MyHurlStack());
      }
    

    当初按照网上的方法只是改了HurlStack里面的部分内容,最后只能得到一个cookie,这里一般都会想到是不是Map的原因。最后查找源码发现真的是这个原因,然后使用了一个笨方法来解决了问题。所以根据源码来分析解决问题还是很需要的。

    相关文章

      网友评论

          本文标题:使用Volley获取多个cookie

          本文链接:https://www.haomeiwen.com/subject/buhcnttx.html