【问题标题】:How to get "Set-Cookie:" value?如何获得“Set-Cookie:”值?
【发布时间】:2019-08-11 19:26:56
【问题描述】:

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

    }
}

【问题讨论】:

标签: java get


【解决方案1】:

你可以试试这个问题的答案: How to get Cookies with HttpURLConnection in Java?

或者从标题中提取 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("*****************************************");
                 }                  
             }             
        }         
    } 
}

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 2018-03-19
    • 2013-03-08
    • 1970-01-01
    • 2012-09-02
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多