java客户端以get方式发送http请求示例(部分参数以*号代替)

package com.yu;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

/**
 * java客户端以get方式发送http请求示例
 *
 * @author Tuzi
 * @version 1.0.0
 **/
public class HttpGetDemo {
    public static void main(String[] args) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = format.format(new Date());

        // 系统参数设置
        Map<String, String> params = new HashMap<>(8);
        params.put("access_token", "**************************");
        params.put("app_key", "***********");
        params.put("method", "task.delete");
        params.put("timestamp", timestamp);

        // 应用参数设置
        params.put("taskId", "7");
        params.put("projectId", "8611");
        String sign = sign("**********************", params);
        params.put("sign", sign);
        timestamp = timestamp.replace(" ", "%20");
        params.put("timestamp", timestamp);
        System.out.println("sign : " + sign);
        String suffixOfUrl = "";
        for (Entry<String, String> entry : params.entrySet()) {
            suffixOfUrl = suffixOfUrl + entry.getKey() + "=" + entry.getValue() + "&";
        }
        suffixOfUrl = suffixOfUrl.substring(0, suffixOfUrl.length() - 1);
        System.out.println("suffixOfUrl : " + suffixOfUrl);
        String url = "http://wop.wangjubao.com/rest/bnAPI?" + suffixOfUrl;

        // 发送http请求
        HttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        String result = null;
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        HttpClientUtils.closeQuietly(client);
        HttpClientUtils.closeQuietly(httpResponse);
        System.out.println("result : " + result);
    }

    /**
     * 构造sign
     */
    private static String sign(String appSecret, Map<String, String> params) {
        Object[] keyArray = params.keySet().toArray();
        List<String> keyList = new ArrayList<>();
        for (Object aKeyArray : keyArray) {
            keyList.add((String) aKeyArray);
        }
        Collections.sort(keyList);
        StringBuilder signContent = new StringBuilder().append(appSecret);

        for (String key : keyList) {
            signContent.append(key).append(params.get(key));
        }
        signContent.append(appSecret);
        return getSign(signContent.toString());
    }

    private static String getSign(String signContent) {
        String hashResult = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(signContent.getBytes("UTF-8"));
            byte byteData[] = md.digest();
            StringBuilder sb = new StringBuilder();
            for (int b : byteData) {
                b = b & 0xFF;
                if (b < 16)
                    sb.append('0');
                sb.append(Integer.toHexString(b));
            }
            hashResult = sb.toString().toUpperCase();
        } catch (Exception ignored) {
        }
        return hashResult;
    }
}

java客户端以post方式发送http请求示例:

package com.yu;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.MessageDigest;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

/**
 * java客户端以post方式发送http请求示例
 *
 * @author Tuzi
 * @version 1.0.0
 **/
public class HttpPostDemo {
    public static void main(String[] args) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = format.format(new Date());

        // 系统参数设置
        Map<String, String> params = new HashMap<>(6);
        params.put("access_token", "**********************");
        params.put("app_key", "****************");
        params.put("method", "task.create");
        params.put("timestamp", timestamp);
        String sign = sign("****************", params);
        params.put("sign", sign);
        timestamp = timestamp.replace(" ", "%20");
        params.put("timestamp", timestamp);
        System.out.println("sign : " + sign);

        String suffixOfUrl = "";
        for (Entry<String, String> entry : params.entrySet()) {
            suffixOfUrl = suffixOfUrl + entry.getKey() + "=" + entry.getValue() + "&";
        }
        suffixOfUrl = suffixOfUrl.substring(0, suffixOfUrl.length() - 1);
        System.out.println(suffixOfUrl);
        String url = "http://wop.wangjubao.com/rest/bnAPI?" + suffixOfUrl;

        System.out.println("url : " + url);
        Map<String, String> taskCreateMap = new HashMap<>(3);

        // 应用参数设置
        taskCreateMap.put("projectId", "8611");
        taskCreateMap.put("contents", "[{\"colId\":\"8612\",\"moduleType\":\"25\",\"value\":\"更新过的标题\"},{\"colId\":\"8613\",\"moduleType\":\"26\",\"value\":\"110119808\"},{\"colId\":\"8614\",\"moduleType\":\"27\",\"value\":\"0\"}]");
        taskCreateMap.put("userId", "10002107");
        taskCreateMap.put("taskId", "6");
        String line = new Gson().toJson(taskCreateMap);

        // 发送http请求
        HttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        String result = null;
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(line, "UTF-8"));
        HttpResponse httpResponse = client.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        System.out.println("输出一下结果 :" + result);
        HttpClientUtils.closeQuietly(client);
        HttpClientUtils.closeQuietly(httpResponse);
    }

    /**
     * 构造sign
     */
    private static String sign(String appSecret, Map<String, String> parames) {
        Object[] keyArray = parames.keySet().toArray();
        List<String> keyList = new ArrayList<>();
        for (Object aKeyArray : keyArray) {
            keyList.add((String) aKeyArray);
        }
        Collections.sort(keyList);
        StringBuilder signContent = new StringBuilder().append(appSecret);

        for (String key : keyList) {
            signContent.append(key).append(parames.get(key));
        }
        signContent.append(appSecret);
        return getSign(signContent.toString());
    }

    private static String getSign(String signContent) {
        String hashResult = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(signContent.getBytes("UTF-8"));
            byte byteData[] = md.digest();
            StringBuilder sb = new StringBuilder();
            for (int b : byteData) {
                b = b & 0xFF;
                if (b < 16)
                    sb.append('0');
                sb.append(Integer.toHexString(b));
            }
            hashResult = sb.toString().toUpperCase();
        } catch (Exception ignored) {
        }
        return hashResult;
    }
}