美文网首页
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 和请求头、响应头

    cookie的获取 cookie的设置: cookie的添加 请求头的添加 注:服务端收到的请求头的key会将你设...

  • JavaWeb--会话Cookie1

    Cookie的实现是基于HTTP协议的,响应头:set-cookie,请求头:cookie Cookie的存活时间...

  • Cookie/Session

    Cookie是什么:Cookie是请求头域和响应头域的字段。简单地说,就是伴随请求和响应的一组键值对的文本。Coo...

  • Electron获取webview中ajax请求内容的方法

    Electron中,可以通过WebRequest监听webview中请求的各个阶段,并且获取或修改请求头和响应头。...

  • Cookie与Session整理记录

    1. Cookie 客户端会话技术,将数据保存到客户端基于响应头的set-cookie和请求头的cookie实现 ...

  • Web存储

    Cookie HTTP Cookie简称cookie,在HTTP请求发送Set-Cookie HTTP头作为响应的...

  • 浅谈http协议

    1.http基础概念 2.请求与响应过程 3.请求头响应头及请求方式简述 4.客户端缓存(cookie,sessi...

  • 请求头和响应头

    1.得到请求头用request.getHeader(String key)。一般的请求头key有user-agen...

  • Cookie 和 Session 机制原理分析 & 区别

    我们来看一个浏览器 console 下面的 http 请求报文信息: 请求头: 响应头: 那么 Cookie 跟 ...

  • cookie详细讲解

    cookie是如何工作的 图中响应头中带有的设置的cookie字段,请求头带有cookie字。请求头中带给后台是浏...

网友评论

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

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