我按照 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”参数。谁能告诉我怎么了?!