`
onlydo
  • 浏览: 165561 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

发送cookie

    博客分类:
  • java
阅读更多

try {
        // Create a URLConnection object for a URL
        URL url = new URL("http://hostname:80");
        URLConnection conn = url.openConnection();
    
        // Set the cookie value to send
        conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
    
        // Send the request to the server
        conn.connect();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }


 try {
        // Create a URLConnection object for a URL
        URL url = new URL("http://hostname:80");
        URLConnection conn = url.openConnection();
    
        // List all the response headers from the server.
        // Note: The first call to getHeaderFieldKey() will implicit send
        // the HTTP request to the server.
        for (int i=0; ; i++) {
            String headerName = conn.getHeaderFieldKey(i);
            String headerValue = conn.getHeaderField(i);
    
            if (headerName == null && headerValue == null) {
                // No more headers
                break;
            }
            if (headerName == null) {
                // The header value contains the server's HTTP version
            }
        }
    } catch (Exception e) {
    }

Here's a sample of headers from a website: 
    Key=Value
    
    null=HTTP/1.1 200 OK
    Server=Netscape-Enterprise/4.1
    Date=Mon, 11 Feb 2002 09:23:26 GMT
    Cache-control=public
    Content-type=text/html
    Etag="9fa67d2a-58-71-3bbdad3283"
    Last-modified=Fri, 05 Oct 2001 12:53:06 GMT
    Content-length=115
    Accept-ranges=bytes
    Connection=close

try {
        // Create a URLConnection object for a URL
        URL url = new URL("http://hostname:80");
        URLConnection conn = url.openConnection();
    
        // Get all cookies from the server.
        // Note: The first call to getHeaderFieldKey() will implicit send
        // the HTTP request to the server.
        for (int i=0; ; i++) {
            String headerName = conn.getHeaderFieldKey(i);
            String headerValue = conn.getHeaderField(i);
    
            if (headerName == null && headerValue == null) {
                // No more headers
                break;
            }
            if ("Set-Cookie".equalsIgnoreCase(headerName)) {
                // Parse cookie
                String[] fields = headerValue.split(";\\s*");
    
                String cookieValue = fields[0];
                String expires = null;
                String path = null;
                String domain = null;
                boolean secure = false;
    
                // Parse each field
                for (int j=1; j<fields.length; j++) {
                    if ("secure".equalsIgnoreCase(fields[j])) {
                        secure = true;
                    } else if (fields[j].indexOf('=') > 0) {
                        String[] f = fields[j].split("=");
                        if ("expires".equalsIgnoreCase(f[0])) {
                            expires = f[1];
                        } else if ("domain".equalsIgnoreCase(f[0])) {
                            domain = f[1];
                        } else if ("path".equalsIgnoreCase(f[0])) {
                            path = f[1];
                        }
                    }
                }
    
                // Save the cookie...
            }
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

Here's a sample of cookies from two websites: 
    B=a43ka6gu6f4n4&b=2; expires=Thu, 15 Apr 2010 20:00:00 GMT;
        path=/; domain=.yahoo.com
    
    PREF=ID=e51:TM=686:LM=86:S=BL-w0; domain=.google.com; path=/;
        expires=Sun, 17-Jan-2038 19:14:07 GMT

// Disable automatic redirects for all HTTP requests
    HttpURLConnection.setFollowRedirects(false);
    
    // Disable automatic redirects for a particular connection
    try {
        // Create a URLConnection object for a URL
        URL url = new URL("http://hostname:80");
        URLConnection conn = url.openConnection();
    
        // Disable automatic redirects just for this connection
        HttpURLConnection httpConn = (HttpURLConnection)conn;
        httpConn.setInstanceFollowRedirects(false);
    
        // Send the request to the server
        conn.connect();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

分享到:
评论

相关推荐

    利用Microsoft.XMLHTTP控件发送COOKIE

    跨站脚本攻击想必各位都已经是很熟悉了,但是得到COOKIE的时候一直有一个 问题:总是要用WINDOW.OPEN弹一个窗体出来然后发送COOKIE,这样隐秘性 就大打折扣了。以前我想了一个在网页中用insertAdjacentHTM

    用header 发送cookie的php代码

    用header 发送cookie的php代码

    cookie_blocker:用于切换发送 Cookie 与否的 Chrome 扩展

    cookie_blocker 用于切换是否发送 Cookie 的 Chrome 扩展程序。 作者 黑川博司 信用和许可证 源代码在 MIT 许可下。 存储库下的所有图标都在来自

    cookie链接提交

    百度cookie链接提交 1.此工具需要百度帐号cookie 2.需要购买代理ip 这是真正实际上看得见的引蜘蛛。跟那些无限提交的不一样.一个账号提交10个链接(一天限制) 网上购买cookie帐号是1快钱400个.相当于提交4000次 好...

    vue项目使用axios发送请求让ajax请求头部携带cookie的方法

    最近做vue项目时遇到登录权限问题,登录以后再发送的请求头部并没有携带登录后设置的cookie,导致后台无法校验其是否登录。检查发现是vue项目中使用axios发送ajax请求导致的。查看文档得知axios默认是不让ajax请求...

    cookie 自动管理

    android cookie 自动管理,包括获取cookie 自动发送cookie等等

    好好了解一下Cookie(强烈推荐)

    服务器像客户端发送cookie 浏览器将cookie保存 之后每次http请求浏览器都会将cookie发送给服务器端 服务器端的发送与解析 发送cookie 服务器端像客户端发送Cookie是通过HTTP响应报文实现的,在Set-Cookie中设置...

    详解Vue用axios发送post请求自动set cookie

    vue-resource不再维护之后,我也用起了axios,但是死活无法设置服务器发送过来的cookie 后来查询文档发现,这个是要单独配置的。 // `withCredentials` indicates whether or not cross-site Access-Control ...

    java邮箱和手机发送链接和验证码

    java向邮箱发激活码和链接 java向手机发验证码 java定时发送 Cookie md5加密 null链接判断

    解决Android webview设置cookie和cookie丢失的问题

    主要介绍了解决Android webview设置cookie和cookie丢失的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    java jsp Cookie学习资料

    java jsp Cookie学习资料 Cookie以键-值对的方式记录信息,服务器利用响应报头Set-Cookie来发送Cookie

    react-cookies:使用React加载和保存Cookie

    类型: object :表示以日期和时间表示的cookie的最长生存期 :表示Cookie的最长生存期,以秒数表示 :指定将向其发送Cookie的那些主机 :每个cookie的范围仅限于一组路径 :将Cookie的范围限制为“安全”渠道...

    命令行工具curl的常用命令汇总.doc

    它可以用于测试API、下载文件、上传文件、发送HTTP基本认证、发送cookie数据、设置请求头等多种用途。Curl支持各种请求方法,包括GET、POST、PUT、DELETE等。它还支持设置请求头、发送表单数据、上传文件、自动跟随...

    jiangbo0216#wiki#cookie跨域共享1

    nginx配置文件#是否允许跨域发送Cookieif ($request_method = 'OPTIONS') {跨域携带发送cookie如果需要允许跨域携带

    PersistJS客户端数据存储的JavaScript框架

    在每个HTTP处理中都要发送cookie会浪费带宽,而且在cookie的创建和修改也都比较复杂。 是什么让PersistJS更好? 很小(gzip压缩之后只有3k) API一致,与所用浏览器无关 无须浏览器插件和其它库 当所用浏览器不支持...

    利用Cookie实现十天免登录

    浏览器接收到来自服务器的Cookie数据之后默认将其保存在浏览器缓存中(如果浏览器关闭,缓存消失,Cookie数据消失),只要浏览器不关闭,当我们下一次发送“特定”请求的时候,浏览器负责将Cookie数据发送给WEB...

    php session和cookie使用说明

    PHP在http协议的头信息里发送cookie, 因此setcookie() 函数必须在其它信息被输出到浏览器前调用,这和对header() 函数的限制类似。1.1 设置cookie: 可以用setcookie() 或setrawcookie() 函数来设置cookie。也可以...

    浅谈COOKIE和SESSION区别

    每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。 1、设置Cookie PHP用SetCookie函数来设置Cookie。 SetCookie函数定义了一个Cookie,并且把它附加在HTTP头的...

Global site tag (gtag.js) - Google Analytics