HttpPostUtil工具類

package com.okni.okpool.okfinance.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import org.apache.http.HttpEntity; 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.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 org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.math.BigDecimal; import java.net.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class HttpPostUtil { /** * 須要導入 json-lib-2.4-jdk15.jar * httpclient-4.3.1.jar * httpcore-4.3.jar * @param parammap 傳參 * @param postpath 接口路徑 * @author mnn * @return JSONObject 調用接口返回數據 * */
    public static JSONObject httpPostTool(Map<String,Object> parammap, String postpath) { JSONObject jsonobject=null; CloseableHttpClient httpclient = HttpClients.createDefault(); // 建立httppost
        HttpPost httppost = new HttpPost(postpath); // 建立參數隊列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Map.Entry<String, Object> entry : parammap.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); } UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { // 調用接口返回的字符串
                    String responseString = EntityUtils.toString(entity, "UTF-8"); jsonobject = JSON.parseObject(responseString); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 關閉鏈接,釋放資源
            try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return jsonobject; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所表明遠程資源的響應結果 */
    public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); URL realUrl = new URL(url); // 打開和URL之間的鏈接
            URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性
            conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Content-type", "application/json; charset=utf-8"); conn.setRequestProperty("Accept", "application/json"); // 發送POST請求必須設置以下兩行
            conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(10*1000); // 獲取URLConnection對象對應的輸出流
            out = new PrintWriter(conn.getOutputStream()); // 發送請求參數
 out.print(param); // flush輸出流的緩衝
 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送 POST 請求出現異常!"+e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流
        finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所表明遠程資源的響應結果 */
    public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url; if(!"".equals(param)){ urlNameString+="?" + param; } URL realUrl = new URL(urlNameString); // 打開和URL之間的鏈接
            URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性
            connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 創建實際的鏈接
 connection.connect(); connection.setConnectTimeout(10*1000); // 獲取全部響應頭字段
            Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷全部的響應頭字段
            for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入流
        finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } public static void main(String[] args) throws IOException, WriterException { // double pr=Math.pow(10,8); // BigDecimal userincome=BigDecimal.valueOf(123.63218633); // System.out.print("==="+userincome.multiply(BigDecimal.valueOf(pr)).longValue());
String content=""; int width = 150; int height = 150; String format = "png"; Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩陣 //將矩陣轉爲Image
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, format, out);//將BufferedImage轉成out輸出流
        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // System.out.print("=========="+ResponseEntity<byte[]>(out.toByteArray(),headers, HttpStatus.CREATED)); // headers, HttpStatus.CREATED)); // return new ResponseEntity<byte[]>(out.toByteArray(), // headers, HttpStatus.CREATED);
 } }
View Code
相關文章
相關標籤/搜索