2010-12-06 9 views
2

PHPカールの次のコードをJAVA HttpURLConnectionに変換するにはどうすればよいですか?PHPカールの次のコードをJAVA HttpURLConnectionに変換するにはどうすればよいですか?

 
$username = $_POST['username']; 
    $headers = array( 
    "Host: mail.google.com", 
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4", 
    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", 
    "Accept-Language: en-us,en;q=0.5", 
    "Accept-Encoding: text", # No gzip, it only clutters your code! 
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", 
    "Date: ".date(DATE_RFC822) 
    ); 
    $c = curl_init('https://mail.google.com/mail/feed/atom'); 
    curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // use authentication 
    curl_setopt($c, CURLOPT_HTTPHEADER, $headers); // send the headers 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // We need to fetch something from a string, so no direct output! 
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); // we get redirected, so follow 
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 1); 
    curl_setopt($c, CURLOPT_UNRESTRICTED_AUTH, 1); // always stay authorised 
    $wrong = curl_exec($c); // Get it 
    curl_close($c); // Close the curl stream 
+0

...など、リクエストヘッダを追加することができますサポートしています?この質問の問題は何ですか? –

+0

問題は実際の明確な質問ではないということです。あなたは自分で問題を解決しようと努力していません。それは "私にtezcodez"の種類を与える。 – Artefacto

+0

私は試していないと言った人。私は何度も試してみましたが、失敗しました。私は助けを求めました...あなたは答えを知っていないと思うので、これをやっています。 –

答えて

3

あなたのGmailの受信トレイのフィードを解析しようとしています。 ローマのRSS/Atomライブラリをこの種のものに使うことができます。

import java.io.IOException; 
import java.io.InputStream; 
import java.io.StringReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.Iterator; 
import java.util.List; 

import org.apache.commons.codec.binary.Base64; 

import com.sun.syndication.feed.synd.SyndEntry; 
import com.sun.syndication.feed.synd.SyndFeed; 
import com.sun.syndication.io.SyndFeedInput; 

public class Gmail { 

    @SuppressWarnings("rawtypes") 
    public static void main(String[] args) throws Throwable { 
     URL url = new URL("https://mail.google.com/mail/feed/atom"); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     String user = "user"; 
     String password = "password"; 
     Base64 base64 = new Base64(); 
     String encodedAuthorization = base64.encodeToString((user + ":" + password).getBytes()); 
     conn.setRequestProperty("Authorization", "Basic " + encodedAuthorization.trim()); 
     InputStream is = conn.getInputStream(); 
     String atomFeed = toString(conn.getInputStream()); 
     SyndFeedInput input = new SyndFeedInput(); 
     SyndFeed feed = input.build(new StringReader(atomFeed)); 
     List entries = feed.getEntries(); 
     Iterator iter = entries.iterator(); 
     while(iter.hasNext()) { 
      SyndEntry entry = (SyndEntry) iter.next(); 
      System.out.println("Mail Subject: " + entry.getTitle()); 
      System.out.println("From: " + entry.getAuthor()); 
     } 
     is.close(); 
     conn.disconnect(); 
    } 

    public static String toString(InputStream in) throws IOException { 
     StringBuffer out = new StringBuffer(); 
     byte[] b = new byte[4096]; 
     for (int n; (n = in.read(b)) != -1;) { 
      out.append(new String(b, 0, n)); 
     } 
     return out.toString(); 
    } 
} 

ローマRSS/Atomのライブラリ:https://rome.dev.java.net/

コモンズコーデック:http://commons.apache.org/codec/

JDOM:http://www.jdom.org/dist/binary/

これらのライブラリーは、スニペットのために必要とされます。

+0

エラーを表示しています:org.jdom.document not found :( –

+0

JDOMライブラリをクラスパスに追加してエラーを修正してください。 – ozhan

+0

このリンクからjarをダウンロードしてください:http://www.ibiblio.org/maven/jdom/jars/jdom-1.0.jar – ozhan

1

使用CommonsのHTTPクライアントライブラリ、それは私がなぜマイナスを知りたいHTTPGET、HttpPostまた、あなたが

関連する問題