我按照Ian Brown的教程为请求http://www.hccp.org/java-net-cookie-how-to.html
设置了一个cookie但它不起作用:
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” - 参数。有人能告诉我什么错了吗?!
答案 0 :(得分:1)
您提到您使用$ _REQUEST来检索Cookie。请注意,$_REQUEST
将检索仅请求参数作为查询字符串或POST请求传递。要检索cookie,请使用$_COOKIE
关联数组。检查this tutorial。