所有来自GAE的Crossdomain

时间:2012-01-07 09:36:13

标签: java gwt gwt-rpc

如何从GWT进行跨域调用? 我发现JSONPRequestBuilder是一个解决方案,但它只能创建GET请求而不是POST。我正在尝试调用谷歌的URL缩短服务(“http://goo.gl/api/shorten”)。

2 个答案:

答案 0 :(得分:1)

从GAE上的servlet,您可以通过URLFetch调用外部http服务。

从客户端GWT,您可以通过gwt-google-apis直接调用Google Shortener API。请参阅页面末尾的shortener example

答案 1 :(得分:0)

通过URLFetch获取。以下是我的代码:

//Classes to import
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;


//Shortening download URL
URL url=new URL("http://goo.gl/api/shorten");
HTTPRequest req=new HTTPRequest(url,HTTPMethod.POST);                               
req.setPayload(("url=www.google.com").getBytes());

URLFetchService  service = URLFetchServiceFactory.getURLFetchService(); 
HTTPResponse response = service.fetch(req); 
byte[] content = response.getContent(); 
String urlshort=new String(content);   //here is the JSON data from goo.gl     
相关问题