1

我按照 Ian Brown 的教程将 cookie 设置为请求http://www.hccp.org/java-net-cookie-how-to.html

但它不起作用:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class cookie {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    URL myUrl = null;
    try {
        myUrl = new URL("http://server/test.php?hlp");
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        URLConnection con = myUrl.openConnection();
        con.setRequestProperty("Cookie", "accesstoken=WERT-DES-COOKIES");
        con.connect();
        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        while((line = reader.readLine()) != null) {
         builder.append(line);
        }
        System.out.println(builder);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

php-test-Script 返回 $_REQUEST-values ......但我只得到 URL 中给出的“hlp”参数。谁能告诉我怎么了?!

4

1 回答 1

1

您提到您正在使用 $_REQUEST 来检索 cookie。请注意,$_REQUEST它将仅检索作为查询字符串或作为 POST 请求传递的请求参数。要检索 cookie,请使用$_COOKIE关联数组。检查本教程

于 2013-06-06T06:55:49.863 回答