我按照伊恩·布朗的教程设置了一个请求的cookiehttp://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测试脚本返回$\u请求值。。。但我只得到url中给定的“hlp”参数。谁能告诉我怎么了?!
1条答案
按热度按时间dnph8jn41#
您提到您正在使用$\请求检索cookies。请注意
$_REQUEST
将仅检索作为查询字符串或post请求传递的请求参数。检索cookie使用$_COOKIE
关联数组。查看本教程。