webmagic request设置POST方法报NullPointerException!
public class MaxSpider implements PageProcessor { private static Site site=Site.me(); static{ site.setCharset("UTF-8"); site.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"); } public static void main(String []args){ Request request=new Request("http://q.maxjia.com/api/bets/join_roll_room/?pkey=&lang=zh-cn"); request.setMethod(HttpConstant.Method.POST); Map<String,Object> map=new HashMap<String,Object>(); map.put("passwd", ""); map.put("room_id", "93628"); request.setExtras(map); Spider.create(new MaxSpider()) .addRequest(request) .run(); } @Override public void process(Page page) { page.putField("join",page.getJson().toString()); } @Override public Site getSite() { return site; } }
2016-12-19 21:33:53,880 ERROR [us.codecraft.webmagic.Spider] - process request Request{url='http://q.maxjia.com/api/bets/join_roll_room/?pkey=&lang=zh-cn', method='POST', extras={statusCode=0, passwd=, room_id=93628}, priority=0} error java.lang.NullPointerException at us.codecraft.webmagic.downloader.HttpClientDownloader.selectRequestMethod(HttpClientDownloader.java:161) at us.codecraft.webmagic.downloader.HttpClientDownloader.getHttpUriRequest(HttpClientDownloader.java:133) at us.codecraft.webmagic.downloader.HttpClientDownloader.download(HttpClientDownloader.java:91) at us.codecraft.webmagic.Spider.processRequest(Spider.java:408) at us.codecraft.webmagic.Spider$1.run(Spider.java:322) at us.codecraft.webmagic.selector.thread.CountableThreadPool$1.run(CountableThreadPool.java:74) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)
//HttpClientDownloader.java:161 if (method == null || method.equalsIgnoreCase(HttpConstant.Method.GET)) { //default get return RequestBuilder.get(); //161就是这一段 } else if (method.equalsIgnoreCase(HttpConstant.Method.POST)) { RequestBuilder requestBuilder = RequestBuilder.post(); NameValuePair[] nameValuePair = (NameValuePair[]) request.getExtra("nameValuePair"); if (nameValuePair != null && nameValuePair.length > 0) { requestBuilder.addParameters(nameValuePair); }
日记打印的明明是method="POST",不知道为什么会跑到那里,而且还报空指针。改成GET方法后就不会了。
刚学webmagic,有点懵.....现在脑袋有点痛,求解救!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
把源代码看了下,弄好了!
@黄亿华
大神救救我!