首页 > 代码库 > httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete

httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete

httpclient post方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//----1、
HttpPost request = new HttpPost(url);
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
param.put("name""rarnu");
param.put("password""123456");
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);
// 发送请求
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = EntityUtils.toString(httpResponse.getEntity());
// 生成 JSON 对象
JSONObject result = new JSONObject( retSrc);
String token = result.get("token");

 

Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete
逻辑:

String url = "http://www.baidu.com";

//将要访问的url字符串放入HttpPost中

HttpPost httpPost = new HttpPost(url);

//请求头 放置一些修改http请求头和cookie

httpPost.setHeader("Accept", "application/json");

......

//如果是HttpPost或者HttpPut请求需要在请求里加参数,而HttpGet或者HttpDelete请求则可以直接拼接到url字符串后面

//向HttpPost中加入参数

List<NameValuePair> values = new ArrayList<NameValuePair>();

values.add(new NameValuePair("id", "1"));

values.add(new NameValuePair("name", "xiaohong"));

httpPost.setEntity(new UrlEncodeFormEntity(values, HTTP.UTF_8));  //进行转码

 

//实例HttpClient 并执行带有HttpPost的方法,返回HttpResponse 响应,再进行操作

HttpClient httpClient = new DefaultHttpClient();

HttpResponse httpResponse = httpClient.execute(httpPost);

int statusCode = httpResponse.getStatusLine().getStatusCode();  //返回状态码 ,用来进行识别或者判断访问结果

if(statusCode == 200){

  Instream in = httpResponse.getEntity().getContent();  //要处理该数据流是否为GZIP流

}

 

 

示例代码如下:

package cn.dratek.haoyingsheng.manager.client;

import cn.dratek.haoyingsheng.manager.util.ResourceUtil;
import net.dratek.browser.http.Cookie;
import net.dratek.browser.http.CookieManager;
import net.dratek.browser.http.URL;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;


public class HttpNetClient {
/**
* 所有get 请求底层调用方法
*
* @param url 请求url
* @return byte[] response data
*/
public static byte[] doGet(String url) {
InputStream in;
byte[] bre = null;
HttpResponse response;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() != 0) {
URL myURL = URL.parseString(url);
Cookie[] cookies = manager.getCookies(myURL);
HttpGet httpGet = new HttpGet(url);
if (cookies != null && cookies.length > 0) {
StringBuilder sb = new StringBuilder();
for (Cookie ck : cookies) {
sb.append(ck.name).append(‘=‘).append(ck.value).append(";");
}
String sck = sb.toString();
if (sck.length() > 0) {
httpGet.setHeader("Cookie", sck);
}

}
httpGet.setHeader("Accept-Encoding", "gzip, deflate");
httpGet.setHeader("Accept-Language", "zh-CN");
httpGet.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
try {
response = new DefaultHttpClient().execute(httpGet);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myURL, header.getValue());
}
}
in = response.getEntity().getContent();
if (in != null) {
bre = ResourceUtil.readStream(in);
}

}
}


} catch (IOException e) {
e.printStackTrace();
}
}


return bre;

}

/**
* 所有Post 请求底层调用方法
*
* @param url 请求url
* @param values 传递的参数
* @return byte[] 返回数据 or null
*/
public static byte[] doPost(String url, List<NameValuePair> values) {
System.out.println("url = " + url);
byte[] bytes = null;
HttpResponse response;
InputStream inputStream = null;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() != 0) {
URL myurl = URL.parseString(url);
Cookie[] cookies = manager.getCookies(myurl);
HttpPost post = new HttpPost(url);
if (cookies != null && cookies.length > 0) {
StringBuilder sb = new StringBuilder();
for (Cookie ck : cookies) {
sb.append(ck.name).append(‘=‘).append(ck.value).append(";");
}
String sck = sb.toString();
if (sck.length() > 0) {
post.setHeader("Cookie", sck);
}

}
post.setHeader("Accept-Encoding", "gzip, deflate");
post.setHeader("Accept-Language", "zh-CN");
post.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
DefaultHttpClient client = new DefaultHttpClient();
try {
if (values != null && values.size() > 0) {
post.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8));
}
response = client.execute(post);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myurl, header.getValue());
}
}
inputStream = response.getEntity().getContent();
}
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (inputStream != null) {
bytes = ResourceUtil.readStream(inputStream);
}
}
return bytes;
}

/**
* PUT基础请求
*
* @param url 请求地址
* @param values 提交参数
* @return byte[] 请求成功后的结果
*/
public static byte[] doPut(String url, List<NameValuePair> values) {
byte[] ret = null;

CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() > 0) {
URL myUrl = URL.parseString(url);
StringBuilder sb = new StringBuilder();
Cookie[] cookies = manager.getCookies(myUrl);
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
sb.append(cookie.name).append("=").append(cookie.value).append(";");
}

}
HttpPut request = new HttpPut(url);
String sck = sb.toString();
if (sck.length() > 0) {
request.setHeader("Cookie", sck);
}
request.setHeader("Accept-Encoding", "gzip, deflate");
request.setHeader("Accept-Language", "zh-CN");
request.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");

DefaultHttpClient client = new DefaultHttpClient();
if (values != null && values.size() > 0) {
try {
UrlEncodedFormEntity entity;
entity = new UrlEncodedFormEntity(values);
request.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
try {
HttpResponse response = client.execute(request);
if (response != null) {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myUrl, header.getValue());
}
}
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
if (inputStream != null) {
ret = ResourceUtil.readStream(inputStream);
inputStream.close();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}

/**
* Delete基础请求
*
* @param url 请求地址
* @return 请求成功后的结果
*/
public static byte[] doDelete(String url) {

InputStream in;
byte[] bre = null;
HttpResponse response;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() != 0) {
URL myurl = URL.parseString(url);
Cookie[] cookies = manager.getCookies(myurl);
HttpDelete delete = new HttpDelete(url);
if (cookies != null && cookies.length > 0) {
StringBuilder sb = new StringBuilder();
for (Cookie ck : cookies) {
sb.append(ck.name).append(‘=‘).append(ck.value).append(";");
}
String sck = sb.toString();
if (sck.length() > 0) {
delete.setHeader("Cookie", sck);
}

}
delete.setHeader("Accept-Encoding", "gzip, deflate");
delete.setHeader("Accept-Language", "zh-CN");
delete.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
try {
response = new DefaultHttpClient().execute(delete);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myurl, header.getValue());
}
}
in = response.getEntity().getContent();
if (in != null) {
bre = ResourceUtil.readStream(in);
}

}
}


} catch (IOException e) {
e.printStackTrace();
}
}


return bre;
}

 

}

 

 

 

 

使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml

[java] view plain copy
 
  1. import java.io.IOException;  
  2. import java.util.List;  
  3.   
  4. import javax.servlet.ServletInputStream;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.   
  8. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;  
  9. import org.apache.commons.httpclient.HttpClient;  
  10. import org.apache.commons.httpclient.HttpStatus;  
  11. import org.apache.commons.httpclient.NameValuePair;  
  12. import org.apache.commons.httpclient.methods.PostMethod;  
  13. import org.apache.commons.httpclient.params.HttpMethodParams;  
  14. import org.apache.commons.io.IOUtils;  
  15. import org.dom4j.Document;  
  16. import org.dom4j.DocumentException;  
  17. import org.dom4j.DocumentHelper;  
  18. import org.dom4j.Element;  
  19. import org.slf4j.Logger;  
  20. import org.slf4j.LoggerFactory;  
  21. import org.springframework.stereotype.Controller;  
  22. import org.springframework.web.bind.annotation.RequestMapping;  
  23. import org.springframework.web.bind.annotation.RequestMethod;  
  24.   
  25. import com.eecn.warehouse.api.model.Address;  
  26. import com.eecn.warehouse.api.model.User;  
  27. import com.eecn.warehouse.utils.FoodBase;  
  28. import com.frogsing.common.api.open.storage.hy.TCustomer;  
  29. import com.frogsing.common.open.tools.xmlUtils;  
  30. import com.google.common.collect.Lists;  
  31. import com.thoughtworks.xstream.XStream;  
  32.   
  33. @Controller  
  34. @RequestMapping(value = {"/api"})  
  35. public class ApiAction {  
  36.     private static Logger logger = LoggerFactory.getLogger(ApiAction.class);  
  37.     public static final String SERVER_URL = "http://192.168.1.100/api/StorageApi";  
  38.       
  39.     @RequestMapping(value = {"/StorageApi"}, method = RequestMethod.POST)  
  40.     public String storageApi(TCustomer model, HttpServletRequest request, HttpServletResponse response) throws DocumentException {  
  41.         String xml = request.getParameter("xml");  
  42.         String signature = request.getParameter("signature");  
  43.         //************************http client*******************//  
  44.         PostMethod postMethod = null;  
  45.         try {  
  46.             // 构造HttpClient的实例  
  47.             HttpClient httpClient = new HttpClient();  
  48.             // 设置连接超时  
  49.             httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);  
  50.             // 设置字符集  
  51.             httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");  
  52.             // 创建POST方法的实例  
  53.             postMethod = new PostMethod(SERVER_URL);  
  54.             // 使用系统提供的默认的恢复策略 该策略在碰到IO异常的时候将自动重试3次。  
  55.             postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 20000);  
  56.             // 异常时,重试处理器  
  57.             postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());  
  58.   
  59.             //参数  
  60.             NameValuePair[] nameValuePairs = new NameValuePair[2];  
  61.             nameValuePairs[0] = new NameValuePair("xml", xml);  
  62.             nameValuePairs[1] = new NameValuePair("signature", signature);  
  63.   
  64.             // 将参数加入到请求方法中  
  65.             postMethod.setRequestBody(nameValuePairs);  
  66.             // 发送连接  
  67.             int statusCode = httpClient.executeMethod(postMethod);  
  68.   
  69.             if (statusCode == HttpStatus.SC_OK) {  
  70.   
  71.                 String result = postMethod.getResponseBodyAsString();  
  72.                 logger.info("返回数据1:" + result);  
  73.                 logger.info("字符集:" + postMethod.getResponseCharSet());  
  74.                 String rspXml = new String(postMethod.getResponseBody(), "UTF-8");  
  75.                 logger.info("返回数据2:" + rspXml);  
  76.   
  77.                 Document document = DocumentHelper.parseText(rspXml);  
  78.                   
  79.                 Element signatureNode = (Element)document.selectSingleNode("//signature");  
  80.                 Element dataNode = (Element)document.selectSingleNode("//data");  
  81.                   
  82.                 String signXml = signatureNode.getText();  
  83.                 String dataXml = dataNode.getText();  
  84.                   
  85.                 String pureXml = FoodBase.decodeXml(dataXml);  
  86.                   
  87.                 request.setAttribute("pureXml", pureXml);  
  88.                 request.setAttribute("signXml", signXml);  
  89.             }  
  90.         } catch (Exception e) {  
  91.             logger.error("http client invoke error.", e);  
  92.         } finally {  
  93.             if (postMethod != null) {  
  94.                 postMethod.releaseConnection();  
  95.             }  
  96.         }  
  97.           
  98.           
  99.         //******************************************************//  
  100.         ServletInputStream inputStream = null;  
  101.         String rspXml = null;  
  102.         try {  
  103.             inputStream = request.getInputStream();  
  104.             rspXml = IOUtils.toString(inputStream, "UTF-8");  
  105.         } catch (IOException e) {  
  106.             logger.error("读取交易仓单返回数据错误.", e);  
  107.         }  
  108.           
  109.         return "api/storage";  
  110.     }  
  111.       
  112.     @RequestMapping(value = {"/MarketApi"}, method = RequestMethod.POST)  
  113.     public String marketApi(HttpServletRequest request, HttpServletResponse response) {  
  114.           
  115.         return "api/market";  
  116.     }  
  117.       
  118.     public static void main(String[] args) {  
  119.         XStream xstream = new XStream();  
  120.         xstream.alias("User", User.class);  
  121.         xstream.alias("Address", Address.class);  
  122.           
  123.         //xStream.aliasAttribute(User.class, "name", "name");  
  124.         xstream.useAttributeFor(User.class, "name");  
  125.           
  126.           
  127.         List<Address> addressList = Lists.newArrayList();  
  128.         for (int i = 0; i < 3; i++) {  
  129.             Address address = new Address();  
  130.             address.setProvince("浙江");  
  131.             addressList.add(address);  
  132.         }  
  133.         User user = new User();  
  134.         user.setAddressList(addressList);  
  135.           
  136.         user.setAccount("asddaa");  
  137.         user.setAge(23);  
  138.         user.setGender(0);  
  139.         user.setName("zhang");  
  140.           
  141.         System.out.println(xstream.toXML(user));  
  142.           
  143.         System.out.println(xmlUtils.toEntityXml(user));  
  144.     }  
  145. }  
get方法类似。
 
 
 
 
 
 
 
 
 
 
 
 

1、httpclient4.3.4 读取网页内容 

特别说明:StreamUtils  只是做了简单的关闭流操作,这个类可有自己写,或者直接调用流关闭操作 谢谢

[java] view plain copy
 
 print?技术分享技术分享
  1. package com.haokuaisheng.https;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5. import java.util.Arrays;  
  6. import java.util.List;  
  7.   
  8. import org.apache.http.Consts;  
  9. import org.apache.http.Header;  
  10. import org.apache.http.HttpEntity;  
  11. import org.apache.http.NameValuePair;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.CloseableHttpResponse;  
  14. import org.apache.http.client.methods.HttpGet;  
  15. import org.apache.http.client.methods.HttpPost;  
  16. import org.apache.http.impl.client.CloseableHttpClient;  
  17. import org.apache.http.impl.client.HttpClients;  
  18. import org.apache.http.message.BasicHeader;  
  19.   
  20. import com.haokuaisheng.log.LogManager;  
  21. import com.haokuaisheng.utils.StreamUtils;  
  22.   
  23.   
  24.   
  25.   
  26. public class HttpUtils {  
  27.       
  28.     final static Header HEADER_USER_AGENT = new BasicHeader( "user-agent" , "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2700.0 Safari/537.36");  
  29.     final static Header HEADER_CONNECTION = new BasicHeader( "connection" , "keep-alive");  
  30.     final static Header HEADER_CACHE_CONTROL = new BasicHeader( "cache-control" , "max-age=0");  
  31.     final static Header HEADER_ACCEPT = new BasicHeader( "accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");  
  32.     final static Header HEADER_ACCEPT_ENCODING = new BasicHeader( "accept-encoding" , "gzip, deflate, sdch");  
  33.     final static Header HEADER_ACCEPT_LANGUAGE = new BasicHeader( "accept-language" , "zh-CN,zh;q=0.8,en;q=0.6");  
  34.     /**  
  35.      * 发送get请求  
  36.      * @param url 请求的url链接  
  37.      * @param charSet该链接字符编码  
  38.      * */  
  39.     public static String requestGet(String url , String charSet , NameValuePair...nameValuePairs){  
  40.         CloseableHttpClient httpclient = HttpClients.createDefault();    
  41.         CloseableHttpResponse response = null ;  
  42.         try {  
  43.             HttpGet httpget = new HttpGet( url );    
  44.             httpget.setHeader( HEADER_USER_AGENT ) ;  
  45.             httpget.setHeader( HEADER_CONNECTION ) ;  
  46.             httpget.setHeader( HEADER_CACHE_CONTROL ) ;  
  47.             httpget.setHeader( HEADER_ACCEPT ) ;  
  48.             httpget.setHeader( HEADER_ACCEPT_ENCODING);  
  49.             httpget.setHeader( HEADER_ACCEPT_LANGUAGE);  
  50.               
  51.             response = httpclient.execute( httpget ) ;  
  52.             if(response.getStatusLine().getStatusCode() == 200){  
  53.                 HttpEntity entity = response.getEntity() ;  
  54.                 return inputStreamToString( entity , charSet );  
  55.             }  
  56.         } catch (Exception e) {  
  57.             LogManager.err("requestGet:" + url , e) ;  
  58.         }finally{  
  59.             try {  
  60.                 response.close();  
  61.                 httpclient.close();  
  62.             } catch (Exception e) {  
  63.             }  
  64.         }  
  65.         return "" ;  
  66.     }  
  67.       
  68.     /** 
  69.      * 发送post请求 
  70.      * @param url 请求的url链接 
  71.      * @param charSet该链接字符编码 
  72.      * */  
  73.     public static String requestPost(String url , String charSet , NameValuePair...nameValuePairs){  
  74.         CloseableHttpClient httpclient = HttpClients.createDefault();    
  75.         CloseableHttpResponse response = null ;  
  76.         try {  
  77.             HttpPost httpPost = new HttpPost( url );    
  78.             httpPost.setHeader( HEADER_USER_AGENT ) ;  
  79.             httpPost.setHeader( HEADER_CONNECTION ) ;  
  80.             httpPost.setHeader( HEADER_CACHE_CONTROL ) ;  
  81.             httpPost.setHeader( HEADER_ACCEPT ) ;  
  82.             httpPost.setHeader( HEADER_ACCEPT_ENCODING);  
  83.             httpPost.setHeader( HEADER_ACCEPT_LANGUAGE);  
  84.               
  85.             List<namevaluepair> formparams = Arrays.asList( nameValuePairs );  
  86.             UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);   
  87.               
  88.             httpPost.setEntity( formEntity ) ;  
  89.             response = httpclient.execute( httpPost ) ;   
  90.             if(response.getStatusLine().getStatusCode() == 200){  
  91.                 HttpEntity entity = response.getEntity() ;  
  92.                 return inputStreamToString( entity , charSet );  
  93.             }  
  94.         } catch (Exception e) {  
  95.             LogManager.err("requestPost:" + url , e) ;   
  96.         }finally{  
  97.             try {  
  98.                 response.close();  
  99.                 httpclient.close();  
  100.             } catch (Exception e) {  
  101.             }  
  102.         }  
  103.         return "" ;  
  104.     }  
  105.       
  106.       
  107.     private static String inputStreamToString(HttpEntity entity , String charSet)  
  108.             throws Exception{  
  109.         InputStream is = null ;   
  110.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  111.         try {  
  112.             is = entity.getContent() ;  
  113.             byte[]bytes = new byte[52428800] ;  
  114.             int buffer = -1;  
  115.               
  116.             while( (buffer = is.read(bytes)) != -1){  
  117.                 baos.write(bytes, 0, buffer) ;  
  118.             }  
  119.             return new String(baos.toByteArray() , charSet ) ;  
  120.         }finally{  
  121.             StreamUtils.closeInput( is );  
  122.             StreamUtils.closeOutput( baos );  
  123.         }  
  124.     }  
  125.       
  126.     public static void main(String[] args) {  
  127.         //String result = requestGet("http://127.0.0.1:8080/a/?age=19" ,  "UTF-8" );  
  128.         //LogManager.info("\n" + result.replace("  
  129. ", "\n"));  
  130.           
  131.         String result = requestGet("http://neihanshequ.com/" ,  "UTF-8" );  
  132.           
  133.           
  134.           
  135.         LogManager.info("\n" + result.replace("  
  136. ", "\n"));  
  137.           
  138.         /* 
  139.         String result = requestPost("http://127.0.0.1:8080/a/" ,  "UTF-8" , new BasicNameValuePair("name" , "中文中文中文")  
  140.         ,  new BasicNameValuePair("age" , "19")  ); 
  141.         LogManager.info(result.replace(" 
  142. ", "\n"));*/  
  143.     }  
  144.       
  145.       
  146. }  
  147.   
  148. </namevaluepair>  

 

 

采用jsoup-1.7.2. 和webmagic-core-0.5.2 jin

 

 

[java] view plain copy
 
 print?技术分享技术分享
  1. package com.haokuaisheng.https;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.haokuaisheng.log.LogManager;  
  6.   
  7. import us.codecraft.webmagic.selector.Html;  
  8.   
  9.   
  10.   
  11. public class XPathUtils {  
  12.       
  13.     public static void main(String[] args) throws Exception {  
  14.           
  15.         String htmlStr = HttpUtils.requestGet("http://neihanshequ.com/", "UTF-8");  
  16.         Html html = new Html( htmlStr ) ;   
  17.         //List<String> titles = html.xpath( "//div[@class=‘post_item_body‘]/h3/a/text()" ).all() ;  
  18.         List<String> titles = html.xpath( "//span[@class=‘name‘]/text()" ).all() ;  
  19.           
  20.         //List<String> bodys = html.xpath( "//div[@class=‘post_item_body‘]/p[@class=‘post_item_summary‘]/text()" ).all() ;  
  21.         List<String> bodys = html.xpath( "//h1[@class=‘title‘]/p/html()" ).all() ;  
  22.         int size = titles.size();  
  23.         for(int x=0; x<size; x++ ){  
  24.             String title = titles.get( x ) ;   
  25.             String body = bodys.get( x );  
  26.             LogManager.info("\n" +  title  + "-->" + body) ;   
  27.         }  
  28.     }  
  29.       

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

其它更深入:

http://www.th7.cn/Program/java/201605/859929.shtml

http://www.tuicool.com/articles/Unumeu

http://blog.csdn.net/binsweet/article/details/5861427

http://blog.csdn.net/csdnbsd/article/details/51850826

http://blog.csdn.net/kl28978113/article/details/51997201

http://blog.csdn.net/kl28978113/article/details/51997201

 

 

 

httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete