java - 如何获取 "Set-Cookie:"值?

标签 java get

我需要该应用程序来显示我当前的 IP 地址。

在本例中,我们有类似的内容(获取请求)

我需要从这里获取IP

public class Main {

public static void main(String[] args) throws Exception {
    String url = "http://2ip.ru/";

    URL obj = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

    connection.setRequestMethod("GET");

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    System.out.println(response.toString());

    }
}

最佳答案

您可以尝试回答这个问题: How to get Cookies with HttpURLConnection in Java?

或者从 header 中提取 cookie。示例(Source):

import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GetCookiesFromHTTPConnection {     
    public static void main(String[] args) throws Exception {         
        URL url = new URL("http://www.google.com:80");
        URLConnection conn = url.openConnection();

        Map<String, List<String>> headerFields = conn.getHeaderFields();

        Set<String> headerFieldsSet = headerFields.keySet();
        Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();

        while (hearerFieldsIter.hasNext()) {             
             String headerFieldKey = hearerFieldsIter.next();

             if ("Set-Cookie".equalsIgnoreCase(headerFieldKey)) {                  
                 List<String> headerFieldValue = headerFields.get(headerFieldKey);

                 for (String headerValue : headerFieldValue) {                      
                    System.out.println("Cookie Found...");

                    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];
                            }
                        }                         
                    }

                    System.out.println("cookieValue:" + cookieValue);
                    System.out.println("expires:" + expires);
                    System.out.println("path:" + path);
                    System.out.println("domain:" + domain);
                    System.out.println("secure:" + secure);

                    System.out.println("*****************************************");
                 }                  
             }             
        }         
    } 
}

关于java - 如何获取 "Set-Cookie:"值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57452956/

相关文章:

javascript - 如何从 Angular 链接获取值(value)

c# - 从 ASP.Net NULL 值输入到 MySQL

jquery - 使用 jQuery 将 JSON 对象发送到新页面

java - 为什么我的父类(super class)的字段在这里被初始化?

java - Apache James 2.3.2 不会写入 MySQL

java - 新用户创建通知邮件不会在 Alfresco 中发送

java - Paypal 网站支付标准未与 Java 集成

java - Hibernate的session线程安全吗?

java - 为什么局部非最终变量会影响引用的字段? java

http - 将 HTTP 响应与其相应的 HTTP 流水线请求相匹配