简体   繁体   English

如何从Java客户端获取httponly cookie?

[英]How to get httponly cookies from a Java client?

how to get httponly cookies? 如何获取httponly cookie? What tools should I use org.apache.http or jsoup, may be url connection... please anybody give me example. 我应该使用什么工具org.apache.http或jsoup,可能是url连接...请任何人给我示例。 I tryed to get cookies using jsoup, but jsoup doesn't return httponly cookies. 我尝试使用jsoup获取cookie,但是jsoup不返回httponly cookie。 Thanks!!! 谢谢!!!

You can use java.net.CookieHandler , referenced at: Java Cookie Handling 您可以使用在Java Cookie处理中引用的java.net.CookieHandler

As an example taken directly from http://www.hccp.org/java-net-cookie-how-to.html : 作为直接从http://www.hccp.org/java-net-cookie-how-to.html取得的示例:

Retrieving cookies from a response: 从响应中获取Cookie:

Open a java.net.URLConnection to the server: 打开与服务器的java.net.URLConnection

URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp");
URLConnection urlConn = myUrl.openConnection();
urlConn.connect();

Loop through response headers looking for cookies: Since a server may set multiple cookies in a single request, we will need to loop through the response headers, looking for all headers named "Set-Cookie". 遍历响应头以查找cookie:由于服务器可能在单个请求中设置多个cookie,因此我们将需要遍历响应头,以查找所有名为“ Set-Cookie”的标头。

String headerName=null;
for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) {
    if (headerName.equals("Set-Cookie")) {                  
    String cookie = urlConn.getHeaderField(i);               
    ...                                                      

Extract cookie name and value from cookie string: The string returned by the getHeaderField(int index) method is a series of name=value separated by semi-colons (;). 从cookie字符串中提取cookie名称和值:getHeaderField(int index)方法返回的字符串是一系列name = value,用分号(;)分隔。 The first name/value pairing is actual data string we are interested in (ie "sessionId=0949eeee22222rtg" or "userId=igbrown"), the subsequent name/value pairings are meta-information that we would use to manage the storage of the cookie (when it expires, etc.). 第一个名称/值配对是我们感兴趣的实际数据字符串(即“ sessionId = 0949eeee22222rtg”或“ userId = igbrown”),后续的名称/值配对是元信息,我们将使用它们来管理cookie的存储(到期时等)。

cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());

This is basically it. 基本上就是这个。 We now have the cookie name ( cookieName ) and the cookie value ( cookieValue ). 现在,我们有了cookie名称( cookieName )和cookie值( cookieValue )。

please anybody give me example 请任何人给我榜样

Here is one with Jsoup. 这是Jsoup的一个。

SAMPLE CODE 样本代码

Response response;
try {
    response = Jsoup //
            .connect("https://httpbin.org/cookies/set?http-only-cookie=test;%20httponly") //
            .ignoreContentType(true) //
            .execute();

    System.out.println(response.cookies());
} catch (IOException e) {
    throw new RuntimeException(e);
}

OUTPUT OUTPUT

{http-only-cookie="test\073 httponly"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM