java HttpsURLConnection 和 Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11494693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpsURLConnection and Cookies
提问by daredesm
Basically i just want to get cookies from server. Here's simple code, but i guess it's not enough to get every cookie like e.g sessionID etc.
基本上我只想从服务器获取 cookie。这是简单的代码,但我想它不足以获取每个 cookie,例如 sessionID 等。
import java.net.*;
import java.util.*;
public class Cookie {
public static void main(String[] args) {
try {
URL url = new URL("http://www.google.com/");
URLConnection conn = url.openConnection();
System.out.println(con.getHeaderField("Set-Cookie"));
} catch (Exception e) { }
}
}
Result shows that i get only one cookie:
结果显示我只得到一个 cookie:
NID=61=nNSsAl4g7DfxqHE7t__ghfoCc_J-RmY7PaTNoPOd_khLdvvuqI-fnteHgnQXMzmxj_C5HdMozcGfOTt8PvePLKpbDdUlzdVZiRKwNpQIej6_T69jt9C7Oi6E4F-gWPHD; expires=Mon, 14-Jan-2013 17:16:40 GMT; path=/; domain=.google.pl; HttpOnly
Firefox gets NID and PREF, so it's not working.
Firefox 获得 NID 和 PREF,因此无法正常工作。
Is there any way to get PREF cookie?
有没有办法获得 PREF cookie?
Can anyone tell me is this proper way(code below) to send POST.
谁能告诉我这是发送 POST 的正确方法(下面的代码)。
import java.io.*;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class Cookie_Example {
public static void main(String[] args){
try{
String https_url = "https://ssl.filmweb.pl/j_login";
URL url = new URL(https_url);
String post = "_login_redirect_url=http%253A%252F%252Fwww.filmweb.pl%252F&j_username=test.filmweb%40gmail.com&j_password=test.filmweb&_rememberMe=on&pass=zaloguj";
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setInstanceFollowRedirects(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Host", "ssl.filmweb.pl");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1");
con.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Language","pl,en-us;q=0.7,en;q=0.3");
con.setRequestProperty("Accept-Encoding","gzip, deflate");
con.setRequestProperty("Connection","keep-alive");
con.setRequestProperty("Referer","https://ssl.filmweb.pl/login");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",post.length()+"");
DataOutput output = new DataOutputStream(new BufferedOutputStream(con.getOutputStream()));
output.write(post.getBytes());
Map<String, List<String>> hf = con.getHeaderFields();
Iterator it = hf.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
} catch (Exception e) {
}
}
}
Result is empty cookie,
结果是空的cookie,
Set-Cookie = [_artuser_rememberMe=;Path=/;Domain=filmweb.pl;Expires=Thu, 01-Jan-1970 00:00:00 GMT]
http://img72.imageshack.us/img72/1134/testgof.jpg
http://img72.imageshack.us/img72/1134/testgof.jpg
Screen from Tamper Data
来自篡改数据的屏幕
It means this code also doesn't work. How Can I get __utma, __utmz etc. cookie? Can anyone help me? Which books will be helpful?
这意味着此代码也不起作用。我如何获得 __utma、__utmz 等 cookie?谁能帮我?哪些书会有帮助?
回答by F.Javier
I think that with your statement, con.getHeaderField("Set-Cookie")"
you only retrieve one header. But maybe the server sends you two cookies, in two different headers.
我认为在您的声明中,con.getHeaderField("Set-Cookie")"
您只能检索一个标题。但也许服务器会在两个不同的标题中向您发送两个 cookie。
I use another statement for retrieve all headers of cookies:
我使用另一个语句来检索 cookie 的所有标头:
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
To see what you send to server, and what the server sends to you, I recommend use a proxy between your program (or web navigator) and the server. Maybe Charles proxy
can help you.
要查看您发送到服务器的内容以及服务器发送给您的内容,我建议在您的程序(或 Web 导航器)和服务器之间使用代理。也许Charles proxy
可以帮到你。