httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
- import java.io.IOException;
- import java.util.List;
- import javax.servlet.ServletInputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.params.HttpMethodParams;
- import org.apache.commons.io.IOUtils;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import com.eecn.warehouse.api.model.Address;
- import com.eecn.warehouse.api.model.User;
- import com.eecn.warehouse.utils.FoodBase;
- import com.frogsing.common.api.open.storage.hy.TCustomer;
- import com.frogsing.common.open.tools.xmlUtils;
- import com.google.common.collect.Lists;
- import com.thoughtworks.xstream.XStream;
- @Controller
- @RequestMapping(value = {"/api"})
- public class ApiAction {
- private static Logger logger = LoggerFactory.getLogger(ApiAction.class);
- public static final String SERVER_URL = "http://192.168.1.100/api/StorageApi";
- @RequestMapping(value = {"/StorageApi"}, method = RequestMethod.POST)
- public String storageApi(TCustomer model, HttpServletRequest request, HttpServletResponse response) throws DocumentException {
- String xml = request.getParameter("xml");
- String signature = request.getParameter("signature");
- //************************http client*******************//
- PostMethod postMethod = null;
- try {
- // 构造HttpClient的实例
- HttpClient httpClient = new HttpClient();
- // 设置连接超时
- httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
- // 设置字符集
- httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
- // 创建POST方法的实例
- postMethod = new PostMethod(SERVER_URL);
- // 使用系统提供的默认的恢复策略 该策略在碰到IO异常的时候将自动重试3次。
- postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 20000);
- // 异常时,重试处理器
- postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
- //参数
- NameValuePair[] nameValuePairs = new NameValuePair[2];
- nameValuePairs[0] = new NameValuePair("xml", xml);
- nameValuePairs[1] = new NameValuePair("signature", signature);
- // 将参数加入到请求方法中
- postMethod.setRequestBody(nameValuePairs);
- // 发送连接
- int statusCode = httpClient.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- String result = postMethod.getResponseBodyAsString();
- logger.info("返回数据1:" + result);
- logger.info("字符集:" + postMethod.getResponseCharSet());
- String rspXml = new String(postMethod.getResponseBody(), "UTF-8");
- logger.info("返回数据2:" + rspXml);
- Document document = DocumentHelper.parseText(rspXml);
- Element signatureNode = (Element)document.selectSingleNode("//signature");
- Element dataNode = (Element)document.selectSingleNode("//data");
- String signXml = signatureNode.getText();
- String dataXml = dataNode.getText();
- String pureXml = FoodBase.decodeXml(dataXml);
- request.setAttribute("pureXml", pureXml);
- request.setAttribute("signXml", signXml);
- }
- } catch (Exception e) {
- logger.error("http client invoke error.", e);
- } finally {
- if (postMethod != null) {
- postMethod.releaseConnection();
- }
- }
- //******************************************************//
- ServletInputStream inputStream = null;
- String rspXml = null;
- try {
- inputStream = request.getInputStream();
- rspXml = IOUtils.toString(inputStream, "UTF-8");
- } catch (IOException e) {
- logger.error("读取交易仓单返回数据错误.", e);
- }
- return "api/storage";
- }
- @RequestMapping(value = {"/MarketApi"}, method = RequestMethod.POST)
- public String marketApi(HttpServletRequest request, HttpServletResponse response) {
- return "api/market";
- }
- public static void main(String[] args) {
- XStream xstream = new XStream();
- xstream.alias("User", User.class);
- xstream.alias("Address", Address.class);
- //xStream.aliasAttribute(User.class, "name", "name");
- xstream.useAttributeFor(User.class, "name");
- List<Address> addressList = Lists.newArrayList();
- for (int i = 0; i < 3; i++) {
- Address address = new Address();
- address.setProvince("浙江");
- addressList.add(address);
- }
- User user = new User();
- user.setAddressList(addressList);
- user.setAccount("asddaa");
- user.setAge(23);
- user.setGender(0);
- user.setName("zhang");
- System.out.println(xstream.toXML(user));
- System.out.println(xmlUtils.toEntityXml(user));
- }
- }
1、httpclient4.3.4 读取网页内容
特别说明:StreamUtils 只是做了简单的关闭流操作,这个类可有自己写,或者直接调用流关闭操作 谢谢
- package com.haokuaisheng.https;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.http.Consts;
- import org.apache.http.Header;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- 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.BasicHeader;
- import com.haokuaisheng.log.LogManager;
- import com.haokuaisheng.utils.StreamUtils;
- public class HttpUtils {
- 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");
- final static Header HEADER_CONNECTION = new BasicHeader( "connection" , "keep-alive");
- final static Header HEADER_CACHE_CONTROL = new BasicHeader( "cache-control" , "max-age=0");
- final static Header HEADER_ACCEPT = new BasicHeader( "accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
- final static Header HEADER_ACCEPT_ENCODING = new BasicHeader( "accept-encoding" , "gzip, deflate, sdch");
- final static Header HEADER_ACCEPT_LANGUAGE = new BasicHeader( "accept-language" , "zh-CN,zh;q=0.8,en;q=0.6");
- /**
- * 发送get请求
- * @param url 请求的url链接
- * @param charSet该链接字符编码
- * */
- public static String requestGet(String url , String charSet , NameValuePair...nameValuePairs){
- CloseableHttpClient httpclient = HttpClients.createDefault();
- CloseableHttpResponse response = null ;
- try {
- HttpGet httpget = new HttpGet( url );
- httpget.setHeader( HEADER_USER_AGENT ) ;
- httpget.setHeader( HEADER_CONNECTION ) ;
- httpget.setHeader( HEADER_CACHE_CONTROL ) ;
- httpget.setHeader( HEADER_ACCEPT ) ;
- httpget.setHeader( HEADER_ACCEPT_ENCODING);
- httpget.setHeader( HEADER_ACCEPT_LANGUAGE);
- response = httpclient.execute( httpget ) ;
- if(response.getStatusLine().getStatusCode() == 200){
- HttpEntity entity = response.getEntity() ;
- return inputStreamToString( entity , charSet );
- }
- } catch (Exception e) {
- LogManager.err("requestGet:" + url , e) ;
- }finally{
- try {
- response.close();
- httpclient.close();
- } catch (Exception e) {
- }
- }
- return "" ;
- }
- /**
- * 发送post请求
- * @param url 请求的url链接
- * @param charSet该链接字符编码
- * */
- public static String requestPost(String url , String charSet , NameValuePair...nameValuePairs){
- CloseableHttpClient httpclient = HttpClients.createDefault();
- CloseableHttpResponse response = null ;
- try {
- HttpPost httpPost = new HttpPost( url );
- httpPost.setHeader( HEADER_USER_AGENT ) ;
- httpPost.setHeader( HEADER_CONNECTION ) ;
- httpPost.setHeader( HEADER_CACHE_CONTROL ) ;
- httpPost.setHeader( HEADER_ACCEPT ) ;
- httpPost.setHeader( HEADER_ACCEPT_ENCODING);
- httpPost.setHeader( HEADER_ACCEPT_LANGUAGE);
- List<namevaluepair> formparams = Arrays.asList( nameValuePairs );
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
- httpPost.setEntity( formEntity ) ;
- response = httpclient.execute( httpPost ) ;
- if(response.getStatusLine().getStatusCode() == 200){
- HttpEntity entity = response.getEntity() ;
- return inputStreamToString( entity , charSet );
- }
- } catch (Exception e) {
- LogManager.err("requestPost:" + url , e) ;
- }finally{
- try {
- response.close();
- httpclient.close();
- } catch (Exception e) {
- }
- }
- return "" ;
- }
- private static String inputStreamToString(HttpEntity entity , String charSet)
- throws Exception{
- InputStream is = null ;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try {
- is = entity.getContent() ;
- byte[]bytes = new byte[52428800] ;
- int buffer = -1;
- while( (buffer = is.read(bytes)) != -1){
- baos.write(bytes, 0, buffer) ;
- }
- return new String(baos.toByteArray() , charSet ) ;
- }finally{
- StreamUtils.closeInput( is );
- StreamUtils.closeOutput( baos );
- }
- }
- public static void main(String[] args) {
- //String result = requestGet("http://127.0.0.1:8080/a/?age=19" , "UTF-8" );
- //LogManager.info("\n" + result.replace("
- ", "\n"));
- String result = requestGet("http://neihanshequ.com/" , "UTF-8" );
- LogManager.info("\n" + result.replace("
- ", "\n"));
- /*
- String result = requestPost("http://127.0.0.1:8080/a/" , "UTF-8" , new BasicNameValuePair("name" , "中文中文中文")
- , new BasicNameValuePair("age" , "19") );
- LogManager.info(result.replace("
- ", "\n"));*/
- }
- }
- </namevaluepair>
采用jsoup-1.7.2. 和webmagic-core-0.5.2 jin
- package com.haokuaisheng.https;
- import java.util.List;
- import com.haokuaisheng.log.LogManager;
- import us.codecraft.webmagic.selector.Html;
- public class XPathUtils {
- public static void main(String[] args) throws Exception {
- String htmlStr = HttpUtils.requestGet("http://neihanshequ.com/", "UTF-8");
- Html html = new Html( htmlStr ) ;
- //List<String> titles = html.xpath( "//div[@class=‘post_item_body‘]/h3/a/text()" ).all() ;
- List<String> titles = html.xpath( "//span[@class=‘name‘]/text()" ).all() ;
- //List<String> bodys = html.xpath( "//div[@class=‘post_item_body‘]/p[@class=‘post_item_summary‘]/text()" ).all() ;
- List<String> bodys = html.xpath( "//h1[@class=‘title‘]/p/html()" ).all() ;
- int size = titles.size();
- for(int x=0; x<size; x++ ){
- String title = titles.get( x ) ;
- String body = bodys.get( x );
- LogManager.info("\n" + title + "-->" + body) ;
- }
- }
其它更深入:
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的主要内容,如果未能解决你的问题,请参考以下文章
Http四种请求方式:post ,get ,put,delete
C#用HttpClient类post get,怎么设置cookies
如何在 Android 中使用 HTTPClient 以 JSON 格式发送 POST 请求?
Android 项目使用 httpclient --> http.client (apache), post/get 方法