java如何获取gmail whitout 403异常的https内容?
如何获取gmail whitout 403异常的https内容
我想获取谷歌https url的内容,但我无法在浏览器中读取响应我有响应,但在我的应用程序中我有403例外 此代码有403异常,但如果在浏览器中打开url,我可以看到我想要的响应,获取此响应并在控制台中打印
public static void main(String args[]) {
try {
String text = "https://www.google.com/accounts/ClientLogin?Content-type=application/x-www-form-urlencoded&accountType=GOOGLE&Email=EMAIL&Passwd=aaaaaa";
URLConnection connection = new URL(text).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
# 1 楼答案
您需要提供用户和密码。你可以通过浏览器直接访问邮件的原因是,gmail使用cookie让你绕过登录屏幕
编辑
最好使用POP或IMAP API以编程方式访问邮箱。 这里有another question on Stackoverflow和library可以帮助你
# 2 楼答案
您可以使用本教程阅读邮件:
http://forum.codecall.net/topic/62143-how-to-read-your-emails-using-java/