Java:使用HttpURLConnection时Cookie丢失
我有这个浏览器类:Java:使用HttpURLConnection时Cookie丢失,java,url,session-cookies,httpurlconnection,Java,Url,Session Cookies,Httpurlconnection,我有这个浏览器类: public class browser{ private List<String> cookies; private HttpURLConnection conn; <....> public String GetPageContent(String url) throws Exception { URL obj = new URL(url); conn = (HttpURLConnection)
public class browser{
private List<String> cookies;
private HttpURLConnection conn;
<....>
public String GetPageContent(String url) throws Exception {
URL obj = new URL(url);
conn = (HttpURLConnection) obj.openConnection();
// default is GET
conn.setRequestMethod("GET");
conn.setUseCaches(false);
// act like a browser
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language",
"en-GB,en;q=0.8,en-US;q=0.6,ro;q=0.4,fr;q=0.2");
if (cookies != null) {
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
System.out.println(cookies);
}
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Get the response cookies
setCookies(conn.getHeaderFields().get("Set-Cookie"));
return response.toString();
}
但当我在我的主程序中执行类似操作时:
users.getList()
这是同一个包中的另一个类,它从Browser类调用GetPageContent方法。事情变得奇怪:大多数请求没有附加cookie。
该类如下所示:
public class Check_List {
private HashMap<String, String> dictionar = new HashMap<String, String>();
public void getList(HashMap dictionar) {
http_handler webpage = new http_handler();
try {
Iterator it = dictionar.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
webpage.GetPageContent("http://example.com/profile.php?id="+pairs.getKey()+"&actionid=15");
System.out.println("I'am on user: " + pairs.getValue());
公共类检查表{
私有HashMap字典=新HashMap();
公共void getList(HashMap字典){
http_处理程序网页=新的http_处理程序();
试一试{
迭代器it=dictionars.entrySet().Iterator();
while(it.hasNext()){
Map.Entry pairs=(Map.Entry)it.next();
System.out.println(pairs.getKey()+“=”+pairs.getValue());
网页。GetPageContent(“http://example.com/profile.php?id=“+pairs.getKey()+”&actionid=15”);
System.out.println(“我在用户上:+pairs.getValue());
你为什么要重新发明轮子?如果你需要一个使用Apache commons HTTP的浏览器类,它已经集成了一个工作的cookie管理系统。你能指出你指的是来自Apache的哪些代码吗?Thanks@MikeS_45由于谷歌搜索的结果。
users.getList()
public class Check_List {
private HashMap<String, String> dictionar = new HashMap<String, String>();
public void getList(HashMap dictionar) {
http_handler webpage = new http_handler();
try {
Iterator it = dictionar.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
webpage.GetPageContent("http://example.com/profile.php?id="+pairs.getKey()+"&actionid=15");
System.out.println("I'am on user: " + pairs.getValue());