package cn.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import cn.utils.HttpUtils;
import cn.utils.xmlUtils;
public class PostXML
{
/**
* @param args
* @throws IOException{
* @throws ClientProtocolException
* @throws DocumentException
*/
public static void main(String[] args)
throws ClientProtocolException, IOException, DocumentException {
//请求的URL
final String URL = "http://search.ifeng.com/searchxml";
final String PREFIX_URL = "http://tech.ifeng.com/digi/mobile/shopping/";
//XML文件输出的位置
final String FILE_PATH = "./XML/response.xml";
//初始化log4j
Logger log = Logger.getLogger(WriteResponse2XML.class);
PropertyConfigurator.configure("./conf/log4j.properties");
HttpClient httpClient = HttpUtils.getHttpClient();
//准备post提交的参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("t", "iPhone"));
list.add(new BasicNameValuePair("u", PREFIX_URL));
list.add(new BasicNameValuePair("n", "2"));
list.add(new BasicNameValuePair("df", "2013-5-1"));
list.add(new BasicNameValuePair("s", "0"));
//执行doPost()请求
HttpResponse response = HttpUtils.doPost(httpClient, URL, list);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
//将结果生成XML文件
Document document = xmlUtils.getDocument(reader);
xmlUtils.document2XML(document, FILE_PATH);
//解析XML文件,并验证其中的关键值
Element rootElement = document.getRootElement();
//获取list节点,验证参数n是否正确
List<Element> itemList = rootElement.elements("item");
if(2 == itemList.size()) {
log.info("请求中参数n=2,也就是每页有2条记录。实际返回的response中也是每页有2条记录。");
}else {
log.info("请求中参数n=2,也就是每页有2条记录。实际返回的response中也是每页有"+itemList.size()+"条记录,与请求参数不符");
}
for(int i=0; i<itemList.size(); i++) {
String title = itemList.get(i).element("Doc_Title").getText();
//System.out.println(title);
//验证参数q是否正确,q是查找的关键字
if(title.contains("iPhone")) {
log.info("请求中参数q=iPhone,也就查找的关键字。实际返回的第"+(i+1)+"条记录中包含该关键字。");
}else {
log.info("第"+(i+1)+"条记录中没有包含该关键字");
}
String url = itemList.get(i).element("Doc_Url").getText();
//验证请求的url是否正确
if(url.startsWith(PREFIX_URL)) {
log.info("实际返回的第"+(i+1)+"条记录中是以指定的URL开始的。");
}else {
log.info("第"+(i+1)+"条记录中没有以指定的URL开始");
}
}
}
}
输出的结果
<?xml version="1.0" encoding="UTF-8"?>
<list>
<numFound>415</numFound>
<max_page>208</max_page>
<QTime>7</QTime>
<item>
<Doc_Date><![CDATA[2013-08-24]]></Doc_Date>
<Doc_Title><![CDATA[冰点价位手机盘点 iPhone 4不足1800元]]></Doc_Title>
<Doc_Url><![CDATA[http://tech.ifeng.com/digi/mobile/shopping/detail_2013_08/24/28981039_0.shtml]]></Doc_Url>
</item>
<item>
<Doc_Date><![CDATA[2013-08-23]]></Doc_Date>
<Doc_Title><![CDATA[本周超值手机大搜罗 iPhone 5仅售3780元]]></Doc_Title>
<Doc_Url><![CDATA[http://tech.ifeng.com/digi/mobile/shopping/detail_2013_08/23/28930491_0.shtml]]></Doc_Url>
</item>
</list>
文档约定
接口名称searchxml:
http://search.ifeng.com/searchxml
参数:
q: 标题字段查询关键词
t;标题字段查询关键词
title:标题字段查询关键词
u: url前缀
df: 起始日期 2012-10-10
dt: 终止日期 2012-11-11
c: 数据类型 1资讯 3博客 4图片 5视频 6话题 8 SEO视频,默认 c=1
n: 返回每页结果数 默认n=10
p:返回第几页 默认 p=1
fl: 返回字段 默认 fl=date,title,url
s: 排序方式,0 按时间排序,按相关度排序s=1或者不传s参数,默认的是按相关度排序
url前缀:
http://tech.ifeng.com/digi/mobile/ 全部
http://tech.ifeng.com/digi/mobile/price/ 行情
http://tech.ifeng.com/digi/mobile/shopping/ 导购
http://tech.ifeng.com/digi/mobile/new/ 新闻
http://tech.ifeng.com/digi/mobile/eva/ 评测