1. 程式人生 > >httpclient 實現的http工具類

httpclient 實現的http工具類

HttpClient實現的工具類

就是簡單的用http 協議請求請求地址並返回資料,廢話少數直接上程式碼

http請求返回的封裝類

    package com.nnk.upstream.util;

    import org.apache.http.cookie.Cookie;

    import java.util.HashMap;
    import java.util.List;

    /**
     * http請求返回結果封裝
     */
    public class HttpResponse {
        //返回的資料
        private
String content=null; //返回的cookies private HashMap<String, String> cookie=null; //重定向的地址 private String location=null; //記錄時間 private String date; //返回的狀態 private int status; public String getContent() { return content; } public
void setContent(String content) { this.content = content; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public HashMap<String, String> getCookie
() { return cookie; } public void setCookie(HashMap<String, String> cookie) { this.cookie = cookie; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } private List<Cookie> cookieList; public List<Cookie> getCookieList() { return cookieList; } public void setCookieList(List<Cookie> cookieList) { this.cookieList = cookieList; } }

httpUtils 工具類

    package com.nnk.upstream.util;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map.Entry;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.config.RequestConfig;
    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.client.protocol.HttpClientContext;
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.AbstractHttpClient;
    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.protocol.BasicHttpContext;
    import org.apache.http.protocol.HttpContext;
    import org.apache.http.util.EntityUtils;
    import org.apache.log4j.Logger;



    /**
     * Http連線工具類
     */
    public class HttpUtil {

        private static Logger log=Logger.getLogger(HttpUtil.class);
        public static RequestConfig getDefConf() {
            return RequestConfig.custom()  //設定連線超時
            .setSocketTimeout(60000)
            .setConnectTimeout(60000)
            .setConnectionRequestTimeout(50000)
            .build();
        }
        /**
         * 傳送get請求並接收返回結果
         * @param url 請求的地址
         * @param charset 編碼方式
         * @return response的bean例項
         */
        public static HttpResponse get(String url,HashMap<String, String> cookie,String charset){
            CloseableHttpClient httpClient=HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();;
            HttpResponse httpResponse=new HttpResponse();
            try{
                HttpGet httpGet=new HttpGet(url);
                StringBuilder reqCookieStr=new StringBuilder();
                Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> entry=iterator.next();
                    reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                httpGet.addHeader("Cookie",reqCookieStr.toString());
                httpGet.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httpGet.addHeader("Connection","Keep-Alive");
                httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httpGet.addHeader("Accept-Encoding","gzip, deflate");
                httpGet.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留著
                httpGet.addHeader("Referer","http://wap.10010.com/t/home.htm");
                CloseableHttpResponse response=httpClient.execute(httpGet);
                HttpEntity entity=response.getEntity();
                int statusCode = response.getStatusLine().getStatusCode();
                log.info("status:"+response.getStatusLine());
                httpResponse.setStatus(statusCode);
                if(statusCode==200||statusCode==302){
                    Header[] responseHeaders=response.getAllHeaders();
                    for(Header header:responseHeaders){
                        if(header.getName().equals("Set-Cookie")){
                            String[] jseeiond = header.getValue().split(";")[0].split("=");
                                cookie.put(jseeiond[0],jseeiond[1]);
                        }else if(header.getName().equals("Location")){
                            httpResponse.setLocation(header.getValue());
                        }
                    }
                    httpResponse.setContent(EntityUtils.toString(entity,charset));
                    httpResponse.setCookie(cookie);
                }else{
                    log.error("訪問Url異常:"+url);
                }
                try{
                    response.close();
                }catch (Exception e) {
                    log.error(e);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return httpResponse;
        }

        /**
         * 傳送post請求並接收返回結果
         * @param url 請求的地址
         * @param cookie 請求的cookie
         * @param nvps post請求的引數
         * @param responseCharSet 返回內容的編碼方式
         * @return 傳送資訊的處理結果
         */
        public static HttpResponse post(String url,HashMap<String, String> cookie,List<NameValuePair> nvps,String responseCharSet){
            CloseableHttpClient httpClient=HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();;
            HttpResponse httpResponse=new HttpResponse();
            try{
                HttpPost httpPost=new HttpPost(url);
                StringBuilder reqCookieStr=new StringBuilder();
                Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> entry=iterator.next();
                    reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                httpPost.addHeader("Cookie",reqCookieStr.toString());
                httpPost.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httpPost.addHeader("Connection","Keep-Alive");
                httpPost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httpPost.addHeader("Accept-Encoding","gzip, deflate");
                httpPost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留著
                httpPost.addHeader("Referer","http://wap.10010.com/t/home.htm");
                httpPost.setEntity(new UrlEncodedFormEntity(nvps,responseCharSet));
                log.info("post datas:" + EntityUtils.toString(httpPost.getEntity()));
                CloseableHttpResponse response=httpClient.execute(httpPost);

                try{
                    HttpEntity entity=response.getEntity();
                    Header[] responseHeaders=response.getAllHeaders();
                    int statusCode = response.getStatusLine().getStatusCode();
                    log.info("status:" + response.getStatusLine());
                    httpResponse.setStatus(statusCode);
                    for (Header header : responseHeaders) {
                        if (header.getName().equals("Set-Cookie")) {
                            Matcher matcher = Pattern.compile("(.+?)=(.*?);\\s{0,1}Domain").matcher(header.getValue());
                            while (matcher.find()) {
                                cookie.put(matcher.group(1), matcher.group(2));
                            }
                        } else if (header.getName().equals("Location")) {
                            httpResponse.setLocation(header.getValue());
                        }
                    }

                    httpResponse.setContent(EntityUtils.toString(entity, responseCharSet));
                    httpResponse.setCookie(cookie);
                }finally{
                    response.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
            }
            return httpResponse;
        }

        /**
         * 傳送post請求並接收返回結果
         * @param url 請求的地址
         * @param cookie 請求的cookie
         * @param nvps post請求的引數
         * @return 傳送資訊的處理結果
         */
        public static HttpResponse downLoad(String url,HashMap<String, String> cookie,List<NameValuePair> nvps,File file){
            CloseableHttpClient httpClient=HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();;
            HttpResponse httpResponse=new HttpResponse();
            try{
                HttpPost httpPost=new HttpPost(url);
                StringBuilder reqCookieStr=new StringBuilder();
                Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> entry=iterator.next();
                    reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                httpPost.addHeader("Cookie",reqCookieStr.toString());
                httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
                httpPost.setEntity(new UrlEncodedFormEntity(nvps,"GB2312"));
                CloseableHttpResponse response=httpClient.execute(httpPost);
                try{
                    HttpEntity entity=response.getEntity();
                    response.getStatusLine().getStatusCode();
                    Header[] responseHeaders=response.getAllHeaders();
                    for(Header header:responseHeaders){
                        if(header.getName().equals("Set-Cookie")){
                            Matcher matcher=Pattern.compile("(.+?)=(.*?);\\s{0,1}Domain").matcher(header.getValue());
                            while(matcher.find()){
                                cookie.put(matcher.group(1),matcher.group(2));
                            }
                        }else if(header.getName().equals("Location")){
                            httpResponse.setLocation(header.getValue());
                        }
                    }
                    FileOutputStream fos=new FileOutputStream(file);
                    fos.write(EntityUtils.toByteArray(entity));
                    fos.close();
                    httpResponse.setCookie(cookie);
                }finally{
                    response.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
            }
            return httpResponse;
        }

        public static String htmlEscape(String content){
            content=content.replace("&amp;","&").replace("&#37;","%").replace("&lt;","<").replace("&gt;",">");
            return content;
        }


        public static HttpResponse sendGet(String url,Cookie[] cookies,String charset) {
            HttpResponse httpResponse = new HttpResponse();
            //1.獲得一個httpclient物件
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();

            CloseableHttpResponse response = null;
            try {
                // 設定cookies
                BasicCookieStore cookieStore = new BasicCookieStore();
                cookieStore.addCookies(cookies);

                HttpContext httpContext = new BasicHttpContext();
                httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

                //2.生成一個get請求
                HttpGet httpget = new HttpGet(url);
                httpget.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httpget.addHeader("Connection","Keep-Alive");
                httpget.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httpget.addHeader("Accept-Encoding","gzip, deflate");
                httpget.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留著
                httpget.addHeader("Referer","http://wap.10010.com/t/home.htm");
                BasicCookieStore cookieStore2;
                //3.執行get請求並返回結果
                response = httpclient.execute(httpget, httpContext);
                cookieStore2 = (BasicCookieStore)httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
                //返回內容設定
                httpResponse.setContent(EntityUtils.toString(response.getEntity(),charset));
                int stauts = response.getStatusLine().getStatusCode();
                //返回狀態設定
                httpResponse.setStatus(stauts);
                //設定返回的Cookie
                List<Cookie> cookies1 = cookieStore2.getCookies();
                cookies1.addAll(Arrays.asList(cookies));
                httpResponse.setCookieList(cookies1);
                Header[] responseHeaders=response.getAllHeaders();
                for(Header header:responseHeaders){
                    if(header.getName().equals("Location")){
                        httpResponse.setLocation(header.getValue());
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                log.info("請求異常.....");
            }finally {
                try {
                    if(null != response) {
                        response.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return httpResponse;
        }


        public static HttpResponse sendPost(String url,Cookie[] cookies,List<NameValuePair> nvps ,String charset) {
            HttpResponse httpResponse = new HttpResponse();
            //1.獲得一個httpclient物件
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();



            CloseableHttpResponse response = null;
            BasicCookieStore cookieStore2;
            try {
                // 設定cookies
                BasicCookieStore cookieStore = new BasicCookieStore();
                cookieStore.addCookies(cookies);
                HttpContext httpContext = new BasicHttpContext();
                httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

                //2.生成一個get請求
                HttpPost httppost = new HttpPost(url);
                httppost.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httppost.addHeader("Connection","Keep-Alive");
                httppost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httppost.addHeader("Accept-Encoding","gzip, deflate");
                httppost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留著
                httppost.addHeader("Referer","http://wap.10010.com/t/home.htm");
                HttpEntity entity = new UrlEncodedFormEntity(nvps,charset);
                log.info("post data:"+EntityUtils.toString(entity));
                httppost.setEntity(entity);
                //3.執行get請求並返回結果
                response = httpclient.execute(httppost, httpContext);
                cookieStore2 = (BasicCookieStore)httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
                //返回內容設定
                httpResponse.setContent(EntityUtils.toString(response.getEntity(),charset));
                int stauts = response.getStatusLine().getStatusCode();
                //返回狀態設定
                httpResponse.setStatus(stauts);
                //設定返回的Cookie
                List<Cookie> cookies1 = cookieStore2.getCookies();
                cookies1.addAll(Arrays.asList(cookies));
                httpResponse.setCookieList(cookies1);
                Header[] responseHeaders=response.getAllHeaders();
                for(Header header:responseHeaders){
                    if(header.getName().equals("Location")){
                        httpResponse.setLocation(header.getValue());
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                log.info("請求異常.....");
            }finally {
                try {
                    if(null != response) {
                        response.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return httpResponse;
        }
    }

封裝重定向的HttpProxyUtils

    package com.nnk.upstream.util;
    import com.nnk.interfacetemplate.common.StringUtil;
    import org.apache.commons.lang3.ArrayUtils;
    import org.apache.http.NameValuePair;
    import org.apache.http.impl.cookie.BasicClientCookie;
    import org.apache.log4j.Logger;

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.*;

    /**
     * Created with IntelliJ IDEA.
     * User: xxydl
     * Date: 2016/11/11
     * Time: 19:12
     * email: [email protected]
     * To change this template use File | Settings | File Templates.
     */
    public class HttpProxyUtil {
        public static final Logger log = Logger.getLogger(HttpProxyUtil.class);
        public static HttpResponse get(String url,List<org.apache.http.cookie.Cookie> cookielist,String charset){
            boolean ret = true;
            HttpResponse response = null;
            do{
                if(StringUtil.isEmpty(url)){
                    log.warn("url is Empty");
                    break;
                }else{
                    log.info("get url:" + url);
                    org.apache.http.cookie.Cookie[] arr = (org.apache.http.cookie.Cookie[])cookielist.toArray(new org.apache.http.cookie.Cookie[cookielist.size()]);
                    response = HttpUtil.sendGet(url, arr, charset);
                }
                if(response!=null && 200==response.getStatus()){
                    return response;
                }else if(response!=null && 302==response.getStatus()){
                    url = response.getLocation();
                    cookielist = response.getCookieList();
                    log.info("重定向到Url:" + url);
                    log.info("cookies:" + response.getCookieList());
                }else {
                    return response;
                }
            }while (true);
            return response;
        }

        public static HttpResponse post(String url,List<org.apache.http.cookie.Cookie> cookielist,List<NameValuePair> nvps,String charset){
            boolean isPost = true;
            HttpResponse response = null;
            do{
                org.apache.http.cookie.Cookie[] arr = (org.apache.http.cookie.Cookie[])cookielist.toArray(new org.apache.http.cookie.Cookie[cookielist.size()]);
                if(StringUtil.isEmpty(url)){
                    log.warn("url is Empty");
                    break;
                }else if(isPost){
                    log.info("post url:" + url);

                    response = HttpUtil.sendPost(url, arr, nvps, charset);
                }else {
                    response = HttpUtil.sendGet(url, arr, charset);
                }
                if(response!=null && 200==response.getStatus()){
                    return response;
                }else if(response!=null && 302==response.getStatus()){
                    url = response.getLocation();
                    log.info("重定向到Url:" + url);
                    log.info("cookies:" + response.getCookieList());
                    cookielist = response.getCookieList();
                    isPost = false;
                }else {
                    return response;
                }
            }while (true);
            return response;
        }

以上有些方法可能沒有自己簡單實現就可以了。

相關推薦

【java】HttpClient實現HTTP檔案通用下載工具

package com.imopan.cps.apart.api.util; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import org.apa

httpclient 實現http工具

HttpClient實現的工具類 就是簡單的用http 協議請求請求地址並返回資料,廢話少數直接上程式碼 http請求返回的封裝類 package com.nnk.upstream.util; import org.apache.http

http工具

工具類 import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOExcept

使用httpclient實現http介面呼叫遇到的坑

最近做一個使用httpclient傳輸json格式引數的介面,使用spring定時器定時觸發呼叫。本來感覺一個簡單的小程式,但是上線之後遇到了很奇葩的問題,執行一段時間後spring定時器莫名的死掉了,然後重啟服務,幾天之後又死掉了。當時第一感覺是不是資料庫連線沒關閉,導致程式死掉,檢查程式碼連線都正常關閉了

HttpClient+圖片載入+工具+介面回撥+AsyncTask封裝到工具

1.MainActivity頁面 package com.example.httpclient; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widg

exp: Http工具

HttpClientUtils.java 見: https://github.com/lifan2/lfLearn/blob/master/src/main/java/com/lflearn/httpLearn/HttpClientUtils.java 幾個問題: 連線池管理器裡

HttpClient+圖片載入+工具+介面回撥

1.MainActivity頁面 package com.example.httpclient; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import

Android : 封裝Http工具,以及日誌攔截器的工具

這個沒有什麼好說的 同標題 package soexample.umeng.com.okhttp.utils; import android.os.Environment; import java.io.File; import java.io.IOException; import

java實現Redis工具 RedisUtil.java

package shop.javaweb.utils; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; imp

Http 工具 連線池 多中請求方式 HttpClient4 Util 引數 XML請求 json 請求 form 表單請求

import com.google.common.base.Strings; import com.google.common.collect.Lists; import org.apache.http.*; import org.apache.http.client.co

JAVA -----HTTP工具

package cn.zhangheng; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.

HttpClient網路連結工具

package com.example.administrator.tv51365.utils; import org.apache.http.HttpResponse; import org.a

Ionic3學習筆記(十三)HttpClient 實現 HTTP 請求以及踩過的一些坑

https://my.oschina.net/metaphors/blog/1589379本文為原創文章,轉載請標明出處目錄貓眼APIHttpClient 實現 HTTP 請求安裝 HttpClientModule 模組建立 provider建立 page一些坑坑1: 未在 

Java 呼叫http介面(基於OkHttp的Http工具方法示例)

目錄 Java 呼叫http介面(基於OkHttp的Http工具類方法示例) OkHttp3 MAVEN依賴 Http get操作示例 Http Post操作示例 Http 超

php7實現http和https請求web服務-通用工具

前段時間做微信開發,因微信有眾多介面呼叫,因此自己整理了一套比較通用的工具類,用以做介面 呼叫,考慮到方便性和簡潔性,這裡選擇使用php的curl擴充套件庫來實現 1 curl啟用和apache的配置 先來看看網友們提供的眾多錯誤方法,本人被這些資料坑的太慘 了 (1)

Java工具--通過HttpClient傳送http請求

在寫網路程式的時候,經常會有從網址獲取資料的需求,上一篇解析JSON就需要從百度獲取天氣資料,本文介紹一種Java傳送http請求的工具–HttpClient。 ##HttpClient的介紹 The most essential function of Ht

基於HttpClient4.5.2實現HttpClient工具

1.maven依賴: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</arti

HttpClient實現的https工具

以下工具類所用到httpClient4.5.jar和log4j.jarimport java.io.IOException; import java.net.URLDecoder; import java.security.KeyManagementException; i

Android開發實現HttpClient工具以及HttpClient的3種超時說明

引子 在Android開發中我們經常會用到網路連線功能與伺服器進行資料的互動,為此Android的SDK提供了Apache的HttpClient來方便我們使用各種Http服務。你可以把HttpClient想象成一個瀏覽器,通過它的API我們可以很方便的發出GET,POST請求(當然它的功能遠不止這些)。

基於httpClients的http請求工具實現restful風格的請求

package com.bx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.ut