本文使用HttpClient发送GET请求,并使用DOM方式获得返回的xml信息。(本人刚开始学习Java,有错误望斧正)
1.使用HttpClient发送GET请求
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SendGetRequest {
public static void main(String[] args) throws IOException {
// 向百度发送GET请求
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet(
"http://www.baidu.com/s?ie=UTF-8&wd=blog.myriadrecord.com");
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
try {
System.out.println(httpResponse.getStatusLine());
HttpEntity httpEntity = httpResponse.getEntity();
//添加处理返回数据的代码
System.out.println(EntityUtils.toString(httpEntity));// 例如将返回的数据转换为字符串并输出
EntityUtils.consume(httpEntity);
} finally {
httpResponse.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
}
2.解析XML
解析的xml文件如下:
485532
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class sendGetRequsetAndShowXMLInfo {
static String URL = "http://8.8.8.8/services/CommonSession/getStoreTree?storeId=0";
public static void main(String[] args) throws Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity) {
InputStream in = entity.getContent();//将返回的内容流入输入流内
EntityUtils.consume(entity);
httpGet.releaseConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(in);//用输入流实例化Document
Element rootElement = document.getDocumentElement();
System.out.println(rootElement.getNodeName() + ": name="
+ rootElement.getAttribute("name"));
NodeList timeNode = rootElement.getElementsByTagName("time");
System.out.println(timeNode.item(0).getNodeName() + ":"
+ timeNode.item(0).getTextContent());
NodeList gradeNode = rootElement.getElementsByTagName("grade");
NodeList classNode = gradeNode.item(0).getChildNodes();
for (int i = 0; i < classNode.getLength(); ++i) {
Element e = (Element) classNode.item(i);
System.out.println(e.getNodeName() + ": name="
+ e.getAttribute("name") + " Number="
+ e.getTextContent());
}
}
}
}
注:之所以xml的格式如此之难看是因为本方法无法跳过空白,
2013-9-9
48
55
32
以上格式的xml文件的缩进空白会同样被视作节点,从而使getChildNodes()返回的NodeList包含空白节点,需要添加额外判断。
参考: