美文网首页
webview cookie 和请求头、响应头

webview cookie 和请求头、响应头

作者: 小川君 | 来源:发表于2018-03-01 15:21 被阅读0次

    cookie的获取

    CookieManager cookieManager = CookieManager.getInstance();
    String        cookieStr     = cookieManager.getCookie(url);
    

    cookie的设置:

    CookieSyncManager.createInstance(context);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();//移除
            cookieManager.setCookie(url, cookie);//cookies是在HttpClient中获得的cookie
            CookieSyncManager.getInstance().sync();
    

    cookie的添加

    CookieManager.getInstance().setCookie(url, Cookie);
    // 同步cookie
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                CookieSyncManager.getInstance().sync();
                return;
            }
            AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
                @Override
                public void run() {
    
                    CookieManager.getInstance().flush();
    
                }
            });
    

    请求头的添加

    Map<String,String> map=new HashMap<>();
    map.put("chuan","测试头");
    webView.loadUrl(url,map);
    

    注:服务端收到的请求头的key会将你设置的key默认改成:HTTP_CHAUN

    响应头的获取

    目前获取响应头的方法只找到了这一种 但是总感觉有些不足 因为在WebResourceResponse类中是有响应头的获取方法的 所以请求大神们多多指教

        public boolean  shouldOverrideUrlLoading  (WebView  view, String  url){  
            // here you will use the url to access the headers.  
            // in this case, the Content-Length one  
            URL url;  
            URLConnection conexion;  
            try {  
                url = new URL(urlConection);  
                conexion = url.openConnection();  
                conexion.setConnectTimeout(3000);  
                conexion.connect();  
                // get the size of the file which is in the header of the request  
                int size = conexion.getContentLength();  
            }  
      
      
            // and here, if you want, you can load the page normally  
            String htmlContent = "";  
            HttpGet httpGet = new HttpGet(urlConection);  
            // this receives the response  
            HttpResponse response;  
            try {  
                response = httpClient.execute(httpGet);  
                if (response.getStatusLine().getStatusCode() == 200) {  
                    // la conexion fue establecida, obtener el contenido  
                    HttpEntity entity = response.getEntity();  
                    if (entity != null) {  
                        InputStream inputStream = entity.getContent();  
                        htmlContent = convertToString(inputStream);  
                    }  
                }  
             } catch (Exception e) {}  
      
             webview.loadData(htmlContent, "text/html", "utf-8");  
             return true;  
        }  
      
        public String convertToString(InputStream inputStream){  
            StringBuffer string = new StringBuffer();  
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));  
            String line;  
            try {  
                while ((line = reader.readLine()) != null) {  
                    string.append(linea + "\n");  
                }  
            } catch (IOException e) {}  
            return string.toString();  
        }
    
    URL obj = new URL(urla);
                            URLConnection conn = obj.openConnection();
                            Map<String, List<String>> map = conn.getHeaderFields();
    
                            System.out.println("显示响应Header信息...\n");
    
                            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                                System.out.println("Key : " + entry.getKey() +
                                        " ,Value : " + entry.getValue());
                            }
    
                            System.out.println("\n使用key获得响应Header信息 \n");
                            List<String> server = map.get("Server");
    
                            if (server == null) {
                                System.out.println("Key 'Server' is not found!");
                            } else {
                                for (String values : server) {
                                    System.out.println(values);
                                }
                            }
    
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    
    

    相关文章

      网友评论

          本文标题:webview cookie 和请求头、响应头

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