2012-01-29 136 views
4

我正在開發一個play Web應用程序,該應用程序應該部署在Google應用程序引擎上。我正在嘗試向另一臺服務器發送請求,而不是處理響應。在我的本地主機它是完美運行然而,當我在GAE上測試時遇到困難。代碼如下:從Google App Engine發送請求

import com.google.appengine.repackaged.org.apache.http.HttpResponse; 
import com.google.appengine.repackaged.org.apache.http.client.methods.HttpGet; 
import com.google.appengine.repackaged.org.apache.http.conn.scheme.PlainSocketFactory; 
import com.google.appengine.repackaged.org.apache.http.conn.scheme.Scheme; 
import com.google.appengine.repackaged.org.apache.http.conn.scheme.SchemeRegistry; 
import com.google.appengine.repackaged.org.apache.http.impl.client.DefaultHttpClient; 
import com.google.appengine.repackaged.org.apache.http.impl.conn.SingleClientConnManager; 
import com.google.appengine.repackaged.org.apache.http.params.BasicHttpParams; 

public class Getter{ 

public static byte[] getStuff(){ 

    String urlString = "http://example.com/item?param=xy"; 

    SchemeRegistry schemeRegistry = new SchemeRegistry(); 
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    BasicHttpParams params = new BasicHttpParams(); 
    SingleClientConnManager connmgr = new SingleClientConnManager(params, schemeRegistry); 
    DefaultHttpClient client = new DefaultHttpClient(connmgr, params); 


    HttpGet get = new HttpGet(urlString); 
    byte[] buf = null; 
    try { 
     HttpResponse resp = client.execute(get); 
     buf = new byte[(int) resp.getEntity().getContentLength()]; 
     resp.getEntity().getContent().read(buf); 
    } catch (Exception e) { 
     System.out.println("There was a problem."); 
     e.printStackTrace(); 
    } 
    return buf; 
} 

} 

可悲的是,我沒有得到任何錯誤消息e.printStackTrace();在GAE上,日誌打印出「有問題」。經過研究,我嘗試了許多實現,但無法讓它們運行。 我欣賞任何形式的幫助。

回答

9

UPDATE:

之前放棄當前的網址提取庫,確保該服務器是公共訪問。請注意,App Engine開發服務器在提出請求時使用您的計算機的網絡配置;因此,如果您試圖從中獲取的URL可以訪問您的網絡,但不在網絡外部,則可能會導致問題。

如果你驗證過的網址確實是公開訪問,那麼請往下看:

與谷歌應用程序引擎抓取的URL在Java中

谷歌應用程序引擎有一個非常清晰的從App Engine發出HTTP請求的要求。雖然其他方法可能從您的本地開發服務器上運行;通常,這些相同的方法不適用於生產。

查看URLFetch文檔。它概述了至少兩種使用低級別URLFetch服務或java.net庫進行HTTP請求的不同方法。

下面是一個例子使用java.net,我已經發現是高度可靠:

import java.net.MalformedURLException; 
import java.net.URL; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 

// ... 
    try { 
     URL url = new URL("http://www.example.com/atom.xml"); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
     String line; 

     while ((line = reader.readLine()) != null) { 
      // ... 
     } 
     reader.close(); 

    } catch (MalformedURLException e) { 
     // ... 
    } catch (IOException e) { 
     // ... 
    } 

HTTP POST:

import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 

    // ... 
    String message = URLEncoder.encode("my message", "UTF-8"); 

    try { 
     URL url = new URL("http://www.example.com/comment"); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); 
     connection.setRequestMethod("POST"); 

     OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); 
     writer.write("message=" + message); 
     writer.close(); 

     if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      // OK 
     } else { 
      // Server returned HTTP error code. 
     } 
    } catch (MalformedURLException e) { 
     // ... 
    } catch (IOException e) { 
     // ... 
    } 
+0

這是偉大的!非常感謝你。你知道從數據中提取響應內容的方法嗎?內容是什麼是一張圖片,我只需要從該方法返回。 – 2012-01-29 10:39:17

+0

同樣,這一切都在Google的URLFetch網站的URLFetch文檔中。我在上面的答案中增加了一些代碼。 – jmort253 2012-01-29 17:37:00