HttpURLConnection與HttpClient區別及使用

區別

1、HttpURLConnection是java的標準類,沒有做封裝,用起來比較原始

2、HttpClient是開源框架,封裝了訪問HTTP的請求頭、參數、內容體、響應等;HttpURLConnection中的輸入輸出流操作,在這個接口中被統一封裝成了HttpPost(HttpGet)和HttpResponse。這樣,減少了操作的繁瑣性。

下面分別給出HttpURLConnection和HttpClient實現GET、POST請求示例,作爲學習。

(1)HttpURLConnection實現GET

package com.jingchenyong.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test_HttpURLConnection_GET
{
    
    public static void main(String[] args) throws Exception
    {
        String urlString="http://15.6.46.35:8080/platform/index.html";
        URL url=new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("connection", "keep-Alive");
        conn.setRequestMethod("GET");
        conn.connect();
        
        int code=conn.getResponseCode();
        System.out.println(code);
        //把流轉爲字符串方式一
        //String  result=IOUtils.toString(conn.getInputStream(),"UTF-8");
        //把流轉爲字符串方式二
        BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String result="";
        String tmp="";
        while((tmp=br.readLine())!=null){
            result=result+tmp;
        }
        System.out.println(result);
        //關閉流和連接
         
    }
    
}

(2)HttpURLConnection實現POST

package com.jingchenyong.test;

import java.io.BufferedOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Test_HttpURLConnection_POST
{
    
    public static void main(String[] args)
        throws Exception
    {
        //設置連接
        String urlString = "http://15.6.46.37:9200//henry/henry/_search";
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("connection", "keep-Alive");
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.connect();
        String jsonstring = getJOSNString();
        if (StringUtils.isNotBlank(jsonstring))
        {
            /**
             * post提交方式一
             */
           /* OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
            writer.write(jsonstring);
            writer.flush();
            writer.close();*/
            BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
            out.write(jsonstring.getBytes());
            out.flush();
            out.close();
            
            //流轉字符串的另一種方式見GET方式
            String  result=IOUtils.toString(conn.getInputStream(),"UTF-8");
            System.out.println(result);
            
            //注意關閉流和連接,這裏省略
        }
    }
    
    public static String getJOSNString()
    {
        JSONObject jsonobject = new JSONObject();
        JSONObject jsonobject1 = new JSONObject();
        JSONObject jsonobject2 = new JSONObject();
        jsonobject2.put("name", "jingchenyong");
        jsonobject1.put("match", jsonobject2);
        jsonobject.put("query", jsonobject1);
        String jsonstring = JSON.toJSONString(jsonobject);
        return jsonstring;
    }
}

(3)HttpClient實現GET

package com.jingchenyong.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.MapUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class Test_HttpClient_GET
{
    
    public static void main(String[] args) throws Exception
    {
        String url="http://15.6.46.35:8080/platform/index.html";
        URIBuilder builder = new URIBuilder(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        Map<String,Object> map=getMap();
        if(MapUtils.isNotEmpty(map)){
            for(Entry<String,Object> entry:map.entrySet()){
                builder.addParameter(entry.getKey(), String.valueOf(entry.getValue()));
            }
            url=builder.build().toString();
        }
        HttpGet get=new HttpGet(url);
        // 增加超時設置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).build();
        get.setConfig(requestConfig);
        
        //發送GET請求
        CloseableHttpResponse response=httpClient.execute(get);
        //獲取狀態碼
        System.out.println(response.getStatusLine().getStatusCode());
        //把結果流轉爲字符串方式一
        /*
        String result=IOUtils.toString(response.getEntity().getContent());
        System.out.println(result);
        */
        //把結果流轉爲字符串方式二
        StringBuilder reMsgBuider = new StringBuilder();
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        String msg = null;
        // 讀取返回消息體
        while ((msg = reader.readLine()) != null) {
            reMsgBuider.append(msg);
        }
        String result = reMsgBuider.toString();
        System.out.println(result);
        //關閉流和連接
        
    }
    public static Map<String,Object> getMap(){
        Map<String,Object> map=new HashMap<String,Object>();
        //map.put("name", "jingchenyong");
        //map.put("age", 26);
        return map;
    }
}

(4)HttpClient實現POST

package com.jingchenyong.test;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Test_HttpClient_POST
{
    
    public static void main(String[] args) throws Exception{
        String jsonstring=getJOSNString();
        RequestConfig config = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();
        CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(config).build();
        HttpPost post = new HttpPost("http://15.6.46.37:9200//henry/henry/_search");
        //填充post體
        post.setEntity(new StringEntity(jsonstring,"UTF-8"));
        CloseableHttpResponse response = client.execute(post);
        System.out.println("狀態碼爲:"+response.getStatusLine().getStatusCode());
        //方式一
        //System.out.println("獲取的結果爲(獲取方式一):"+IOUtils.toString(response.getEntity().getContent()));
        //方式二 見GET
        //方式三
        System.out.println("獲取的結果爲(獲取方式二):"+EntityUtils.toString(response.getEntity(), "UTF-8"));
        
    }
    public static String getJOSNString()
    {
        JSONObject jsonobject = new JSONObject();
        JSONObject jsonobject1 = new JSONObject();
        JSONObject jsonobject2 = new JSONObject();
        jsonobject2.put("name", "jingchenyong");
        jsonobject1.put("match", jsonobject2);
        jsonobject.put("query", jsonobject1);
        String jsonstring = JSON.toJSONString(jsonobject);
        return jsonstring;
    }
}
性能方面:
HttpURLConnection的訪問速度比HttpClient要快。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章