无线开放 API

FetchUrl服务简介

TAE 中 FetchUrl服务提供基于http协议的网页抓取。目前TAE系统中有全局 FetchUrl的白名单。如果期望的URL不在全局白名单中,可自主申请App粒度的URL白名单。值得注意的是,HTTP请求必须使用FetchUrl,之外的类似HttpClient之类的API不被允许。

FetchUrl服务API

本节对FetchUrl服务API的使用方式进行介绍。关于详细的API说明,请 参考Java Doc

引入服务包

获取FetchUrl服务对象

    import com.alibaba.appengine.api.fetchurl.FetchUrlService;
import com.alibaba.appengine.api.fetchurl.FetchUrlServiceFactory;
FetchUrlService fetchUrlService = FetchUrlServiceFactory.getFetchUrlService();

fetchUrlService.get(url) 方法

    /**
 * 对指定的url,发起HTTP GET请求,获取HTTP相应的内容
 * @param url HTTP URL地址  不能为null
 * @return 响应内容
 * @throws FetchUrlNotAllowedException url为非HTTP协议、非白名单域名
 * @throws FetchUrlException           操作出错,如连接FetchUrl Server失败
 * @throws NullPointerException        url参数为null
 */
 String get(String url);

fetchUrlService.post(url,params) 方法

    /**
 * @param url               HTTP URL地址  不能为null
 * @param requestParameters 请求参数Map如果为null,则表示空参数
 * @return 响应内容
 * @throws FetchUrlNotAllowedException url为非HTTP协议、非白名单域名
 * @throws FetchUrlException           出错,如连接FetchUrl Server失败
 * @throws NullPointerException        url参数为null
 */
 * 对指定的url,发起HTTP POST请求,获取HTTP相应的内容,请求参数编码默认为 UTF-8
 String post(String url, Map requestParameters); 

fetchUrlService.post(url,params,charset) 方法

    /**
 * 对指定的url,发起HTTP POST请求,获取HTTP相应的内容
 * @param url               HTTP URL地址  不能为null
 * @param requestParameters 请求参数Map如果为null,则表示空参数
 * @param parameterCharset  请求参数的字符编码,取值范围 UTF-8 | GBK | ISO-8859-1,默认值为 UTF-8
 * @return 响应内容
 * @throws FetchUrlNotAllowedException url为非HTTP协议、非白名单域名
 * @throws FetchUrlException           出错,如连接FetchUrl Server失败
 * @throws NullPointerException        url参数为null
 */
 String post(String url, Map requestParameters, String parameterCharset); 

fetchUrlService.getBytes(url)方法

    /**
 * 对指定的url,发起HTTP GET请求,获取HTTP相应的内容
 * 
 * @param url HTTP URL地址  不能为null
 * @return 响应内容
 * @throws FetchUrlNotAllowedException url为非HTTP协议、非白名单域名
 * @throws FetchUrlException           操作出错,如连接FetchUrl Server失败
 * @throws NullPointerException
 * @since 1.1.1
 * /
byte[] getBytes(String url);

FetchUrl服务域名限制

为了规范TAE中isv对淘宝站内站外的域名访问情况,后面FetchUrl服务的全局白名单只留下下面四个:

eco.taobao.com

login.taobao.com

a.tbcdn.cn/app/sns/*

pin.aliyun.com

img.taobaocdn.com

api.weibo.com

oauth.taobao.com 

其它需要访问的域名配置到对应应用的白名单中,需要在TAE控制台里申请添加。申请添加的域名个数限制为5个,审核时间在一个工作日左右。

注意:Java不再支持使用httpclient、httpconnection等调用站外;Php不再支持使用curl函数调用站外。TAE中top接口调用不在此限制,如有对应的站外调用需求,请改为使用fetchurl服务. 

FetchUrl服务使用示例

请参考 基础服务Demo