httpclient4.5的get和post方法
来源:互联网 发布:淘宝网iphone6 无指纹 编辑:程序博客网 时间:2024/06/06 10:30
package an;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.client.methods.RequestBuilder;import org.apache.http.cookie.Cookie;import org.apache.http.impl.client.BasicCookieStore;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;public class PostForm {/* * 自动填单 */public String hcPost() throws ClientProtocolException, IOException, URISyntaxException {BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCookieStore(cookieStore) .build(); try { HttpGet httpget = new HttpGet("https://passport.csdn.net/account/login"); CloseableHttpResponse response1 = httpclient.execute(httpget); try { HttpEntity entity = response1.getEntity(); System.out.println("Login form get: " + response1.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response1.close(); } HttpUriRequest login = RequestBuilder.post() .setUri(new URI("https://passport.csdn.net/account/login")) .addParameter("username", "my") .addParameter("password", "123") //.addParameter("IDToken2", "password") .build(); CloseableHttpResponse response2 = httpclient.execute(login); try { HttpEntity entity = response2.getEntity(); System.out.println("Login form get: " + response2.getStatusLine()); EntityUtils.consume(entity); System.out.println("Post logon cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response2.close(); } } finally { httpclient.close(); } return "yes";}public static void main(String[]args) throws Exception{PostForm pf=new PostForm(); String ret=pf.hcPost(); System.out.println("********"+ret); }}
httpclient提交后用htmlparser解析内容:
package an;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import org.apache.http.HttpEntity;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.client.methods.RequestBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.htmlparser.Parser;import org.htmlparser.beans.StringBean;import org.htmlparser.util.ParserException;public class PostForm {public String hcPost() throws ClientProtocolException, IOException, URISyntaxException, ParserException, ParseException { CloseableHttpClient httpclient = HttpClients.custom().build(); HttpUriRequest post = RequestBuilder.post() .setUri(new URI("http://mail.163.com/")) .addParameter("username", "my") .addParameter("password", "123") //.addParameter("IDToken2", "password") .build(); CloseableHttpResponse response = httpclient.execute(post); try { HttpEntity entity = response.getEntity();//中文乱码 if( response.getStatusLine().getStatusCode() == 200){ StringBean sb = new StringBean (); sb.setLinks (false);//设置不需要得到页面所包含的链接信息 sb.setReplaceNonBreakingSpaces(true);//设置将不间断空格由正规空格所替代 sb.setCollapse(true);//设置将一序列空格由一个单一空格所代替 Parser parser = new Parser(); parser.setInputHTML(EntityUtils.toString(entity)); parser.reset (); parser.visitAllNodesWith (sb); String text = sb.getStrings (); return text; } } finally { response.close();} return response.getStatusLine().toString();}public static void main(String[]args) throws Exception{PostForm pf=new PostForm(); String ret=pf.hcPost(); System.out.println("********"+ret); }}
0 0
- httpclient4.5的get和post方法
- httpclient4 发送http请求的get和post用法
- 关于HttpClient4.3.*的Post和Get提交乱码问题
- HttpClient4.x:Get和Post提交数据
- httpclient4.3发送post和get实例代码
- GET方法和POST方法的区别
- GET方法和POST方法的区别
- GET方法和POST方法的区别
- Get方法和Post方法的区别
- GET方法和POST方法的区别
- Get方法和Post方法的区别
- GET方法和POST方法的区别
- get和post方法
- Httpclient4.3.5简单get,post实用(httpcomponents-client-4.3.5)
- Post和Get方法的区别
- Http的get和post方法比较
- HttpURLConnection的post和Get方法
- Get和Post方法的区别
- Summary Ranges--JavaScript
- MongoDB权威指南-第8章
- iOS开发---如何给类别增加属性
- spring(基础10) 注解@Value详解
- codeigniter框架初次接触
- httpclient4.5的get和post方法
- 数据还原指定日期
- mysql截取字符串值修改字段值
- 沉思的片刻,我们都是哲学家
- 工作-stringbuffer
- Powershell 工作流实战(1)
- JS数组与字符串转换(包括数组中含有对象)
- iOS 提交错误
- android RecyclerView获得单个Item的ViewHolder