Commit 49de9374 authored by yihua.huang's avatar yihua.huang

new SimpleHttpClient #576

parent 7ffc6998
......@@ -93,12 +93,14 @@ public class Request implements Serializable {
return extras;
}
public void setExtras(Map<String, Object> extras) {
public Request setExtras(Map<String, Object> extras) {
this.extras = extras;
return this;
}
public void setUrl(String url) {
public Request setUrl(String url) {
this.url = url;
return this;
}
/**
......@@ -111,8 +113,9 @@ public class Request implements Serializable {
return method;
}
public void setMethod(String method) {
public Request setMethod(String method) {
this.method = method;
return this;
}
@Override
......
package us.codecraft.webmagic;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.model.PageMapper;
import us.codecraft.webmagic.proxy.ProxyProvider;
/**
* @author code4crafter@gmail.com
* Date: 2017/5/27
* @since 0.7.0
*/
public class SimpleHttpClient {
private final HttpClientDownloader httpClientDownloader;
private final Site site;
public SimpleHttpClient() {
this(Site.me());
}
public SimpleHttpClient(Site site) {
this.site = site;
this.httpClientDownloader = new HttpClientDownloader();
}
public void setProxyProvider(ProxyProvider proxyProvider){
this.httpClientDownloader.setProxyProvider(proxyProvider);
}
public <T> T get(String url, Class<T> clazz) {
return get(new Request(url), clazz);
}
public <T> T get(Request request, Class<T> clazz) {
Page page = httpClientDownloader.download(request, site.toTask());
if (!page.isDownloadSuccess()) {
return null;
}
return new PageMapper<T>(clazz).get(page);
}
public Page get(String url) {
return httpClientDownloader.download(new Request(url), site.toTask());
}
public Page get(Request request) {
return httpClientDownloader.download(request, site.toTask());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment