0

how can get https content of gmail whitout 403 exception?

i want get content of an https url of google but i cannot read the response in my browser i have response but in my app i have 403 exception this code have 403 exception but if open the url in browser i can see the response i want get this response and print in console

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();

    }
user2601734
  • 575
  • 1
  • 5
  • 12
  • 8
    If you want to get at mails, why not [use IMAP](https://developers.google.com/gmail/oauth_overview) and avoid all of that screen scraping you're about to do? – Philipp Reichart Aug 06 '13 at 11:14

2 Answers2

0

You need to provide a user and a password. The reason why you can access your mail directly with the brower is because gmail uses cookies to allow you to bypass log in screen.

EDIT

It is much better to use POP or IMAP API to programtically access your mailbox. Here is another question on Stackoverflow and a library that could help you.

Community
  • 1
  • 1
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
0

You can read mail using this tutorial:

http://forum.codecall.net/topic/62143-how-to-read-your-emails-using-java/

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
Renish Khunt
  • 5,620
  • 18
  • 55
  • 92