zoukankan      html  css  js  c++  java
  • 常用工具类

    1.发送post请求,传输json数据

    package net.shopxx.util;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Map;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    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.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    import okhttp3.OkHttpClient;
    
    public class HttpRequestUtil {
        
        public static JSONObject doPost(String url, JSONObject json){
    
            CloseableHttpClient httpclient = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(url);
            JSONObject response = null;
            try {
                StringEntity s = new StringEntity(json.toString());
                s.setContentEncoding("UTF-8");
                s.setContentType("application/json");//发送json数据需要设置contentType
                post.setEntity(s);
                HttpResponse res = httpclient.execute(post);
                if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                    response = JSON.parseObject(result);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            System.out.println(response);
            return response;
        }
    
        public static void main(String[] args) {
            //String url="http://www.laotinghuagong.com/event/online";
            String url="http://localhost:8083/gdsk/event.do?online";
            //appid=190920753389&sign=82D51AB250C38800B70B2F82440464DB&imei=363620190803947
            //&msgId=dadf7a7fd3454e5facde2d450957a275&type=wifi&address=23423423234&time=2156633&lng=119.3255211&lat=39.552455
            JSONObject jsondata=new JSONObject();
            jsondata.put("appid", "190920753389");
            jsondata.put("sign", "82D51AB250C38800B70B2F82440464DB");
            jsondata.put("imei", "363620190803947");
            jsondata.put("msgId", "dadf7a7fd3454e5facde2d450957a275");
    //        jsondata.put("type", "wifi");
    //        jsondata.put("address", "23423423234");
    //        jsondata.put("time", "2156633");
    //        jsondata.put("lng", "119.3255211");
    //        jsondata.put("lat", "39.552455");
            //
            JSONObject doPost = doPost(url, jsondata);
        }
    }

     2.微信签名 其中Key为签名

        /**
         * 微信支付签名算法sign
         * @param characterEncoding
         * @param parameters
         * @return
         */
        @SuppressWarnings("unchecked")
        public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
            StringBuffer sb = new StringBuffer();
            Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)
            Iterator it = es.iterator();
            while(it.hasNext()) {
                Map.Entry entry = (Map.Entry)it.next();
                String k = (String)entry.getKey();
                Object v = entry.getValue();
                if(null != v && !"".equals(v) 
                        && !"sign".equals(k) && !"key".equals(k)) {
                    sb.append(k + "=" + v + "&");
                }
            }
            sb.append("key=" + Key);
            String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
            return sign;
        }

    MD5

    package com.jeecg.util;
    
    import java.security.MessageDigest;
    
    public class MD5Util {
        private static String byteArrayToHexString(byte b[]) {
            StringBuffer resultSb = new StringBuffer();
            for (int i = 0; i < b.length; i++)
                resultSb.append(byteToHexString(b[i]));
     
            return resultSb.toString();
        }
     
        private static String byteToHexString(byte b) {
            int n = b;
            if (n < 0)
                n += 256;
            int d1 = n / 16;
            int d2 = n % 16;
            return hexDigits[d1] + hexDigits[d2];
        }
     
        public static String MD5Encode(String origin, String charsetname) {
            String resultString = null;
            try {
                resultString = new String(origin);
                MessageDigest md = MessageDigest.getInstance("MD5");
                if (charsetname == null || "".equals(charsetname))
                    resultString = byteArrayToHexString(md.digest(resultString
                            .getBytes()));
                else
                    resultString = byteArrayToHexString(md.digest(resultString
                            .getBytes(charsetname)));
            } catch (Exception exception) {
            }
            return resultString;
        }
     
        private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    }
  • 相关阅读:
    导弹拦截版
    [USACO1.5]数字三角形 Number Triangles
    FBI树
    修复公路
    台阶问题
    阶乘问题
    连续自然数和
    又是毕业季I
    生活大爆炸版石头剪刀布
    曹冲养猪
  • 原文地址:https://www.cnblogs.com/longsanshi/p/11772043.html
Copyright © 2011-2022 走看看