也是因为那个采集项目的需要. 很失落地发现Adobe AIR的XMLHttpRequest也好, URLLoader也好, 都不支持设置代理, 一般似乎是用的系统设置. 于是自己实现了一个, 比较简单, 不支持HTTPS, 但是cookie什么的都是做到位了的. 有responseText和responseBody, 分别用于读取文本和字节文件, 如图片. 暂不支持压缩, 不过chunked是支持的.
代码已经传到github上了, 这里上一段实例, 当然, 只用到少数功能. 顺便这个是基于VEJIS, 如果不想或者不会用VEJIS可以自己稍作修改.
这里是VEJIS https://github.com/vilic/vejis
这里是这个HTTP请求 https://github.com/vilic/air-proxy-enabled-http-request-for-js
use_("http-request", function (hr) {
var req = new hr.Request();
//you can turn off cookies
//req.cookieEnabled = false;
//or turn off auto redirect
//req.autoRedirect = false;
req.proxy.host = "localhost";
req.proxy.port = 1107;
req.open("get", "http://www.vilic.info/blog/");
//you can set request headers
//req.setRequestHeader("Referer", "http://www.vilic.info/");
req.send(function (req) {
if (req.error) {
alert(req.error);
return;
}
alert(req.status);
alert(req.responseText);
});
//if you use post, you'll also need to send the data
//string and ByteArray are supported
//req.send(data, callback);
});
