2013-08-06 43 views
0

怎樣才能獲得gmail的whit內容的whitout 403異常?如何才能獲得gmail的whit內容的whitout 403異常?

我想獲取谷歌的https網址的內容,但我無法在我的瀏覽器中讀取響應我有迴應,但在我的應用程序中,我有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(); 

    } 
+8

如果你想獲得的郵件,爲什麼不[使用IMAP(https://developers.google.com/gmail/oauth_overview)和避免你要做的那個屏幕抓取? –

回答

0

您需要提供用戶名和密碼。您可以直接使用瀏覽器訪問您的郵件的原因是,gmail使用cookie允許您繞過登錄屏幕。

編輯

最好就是使用POP或IMAP API來訪問程式設計您的郵箱。 這裏是another question on Stackoverflowlibrary可以幫助你。