HttpPostUtil工具类

匿名 (未验证) 提交于 2019-12-02 23:49:02
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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!