繁体   English   中英

如何获得“Set-Cookie:”值?

[英]How to get "Set-Cookie:" value?

我需要应用程序来显示我当前的 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?

或者从标题中提取 cookie。 示例( 来源):

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("*****************************************");
                 }                  
             }             
        }         
    } 
}

暂无
暂无

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

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