com.kingja.springmvc.util.HttpRequestUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.kingja.springmvc.util.HttpRequestUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kingja.springmvc.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author huboliang
 */
public class HttpRequestUtils {

    /**
     * ??HttpWeb
     *
     * @param path Web?
     * @param map Http?
     * @param encode ??
     * @return Web?
     */
    public String sendHttpClientPost(String path, Map<String, String> map, String encode) {
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        if (map != null && !map.isEmpty()) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                //?Map?BasicNameValuePair?
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }

        try {
            // ???HttpEntity
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode);
            //HttpPost?
            HttpPost httpPost = new HttpPost(path);
            //?Form
            httpPost.setEntity(entity);
            //HttpAndroidHttpClient
            CloseableHttpClient httpclient = HttpClients.createDefault();
            //??
            HttpResponse httpResponse = httpclient.execute(httpPost);
            //??200??
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                //HttpEntity??
                InputStream inputStream = httpResponse.getEntity().getContent();
                return changeInputStream(inputStream, encode);
            }

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "";
    }

    /**
     * Web????
     *
     * @param inputStream ??
     * @param encode ??
     * @return ??
     */
    private String changeInputStream(InputStream inputStream, String encode) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        String result = "";
        if (inputStream != null) {
            try {
                while ((len = inputStream.read(data)) != -1) {
                    outputStream.write(data, 0, len);
                }
                result = new String(outputStream.toByteArray(), encode);

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

    public String get(String path) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // httpget.    
            HttpGet httpget = new HttpGet(path);
            // get.    
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // ??    
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toString(entity);
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // ,?    
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
}