Java 调用示例

package httptest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;

import org.apache.commons.codec.binary.Hex;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

/***
 * 上传附件
 * 
 * @author 0530
 *
 */
public class MailapiRequestDemo {
    public static String appKey = "da1f4cddca568b9337d793b5e520c6204b6556b7";
    public static String uploadFileUrl = "http://10.10.255.196/api/v2/media/upload";
    public static String sendQywxTextUrl = "http://10.10.255.196/api/v2/qywxsend/text";

    /***
     * Get 请求
     * @param requestUrl 实际请求链接
     */
    public static void doGet(String requestUrl) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForEntity(requestUrl, String.class).getBody();
            System.out.println(result);
        } catch (HttpStatusCodeException e) {
            e.printStackTrace();
            System.out.println(e.getResponseBodyAsString());
        }
    }
    /***
     * Post 请求
     * 
     * @param url        请求地址
     * @param to_objects 目的接收人 String or List<String>
     * @param sendtext   发送内容(单一)
     */
    public static void doPost(String url, Object to_objects, String sendtext) {
        // prepare request body
        JSONObject param = new JSONObject();
        param.put("key", appKey);
        param.put("to", to_objects);
        param.put("body", sendtext);

        // prepare headers
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

        // get request entity
        HttpEntity<String> requestEntity = new HttpEntity<>(param.toString(), headers);
        try {
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.postForEntity(url, requestEntity, String.class).getBody();
            System.out.println(result);
        } catch (HttpStatusCodeException e) {
            e.printStackTrace();
            System.out.println(e.getResponseBodyAsString());
        }

    }

    /***
     * 上传附件
     * 
     * @param media_path 文件路径
     */
    public static void doPostUpload(String url, String media_path) {
        File file = new File(media_path);

        // prepare request body
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", MediaType.TEXT_PLAIN_VALUE);
        headers.set("Accept", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        headers.set("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
        headers.set("Content-MD5", getMD5(file));
        headers.set("Content-Length", String.valueOf(file.length()));
        headers.set("key", appKey);

        FileSystemResource resource = new FileSystemResource(new File(media_path));
        // get request entity
        HttpEntity<FileSystemResource> requestEntity = new HttpEntity<>(resource, headers);
        try {
            RestTemplate restTemplate = new RestTemplate();
            String res = restTemplate.postForEntity(url, requestEntity, String.class).getBody();
            System.out.println(res);
        } catch (HttpStatusCodeException e) {
            e.printStackTrace();
            System.out.println(e.getResponseBodyAsString());
        }

    }

    /***
     * 获取文件MD5
     * 
     * @param file 要上传的文件对象
     * @return 文件MD5
     */
    public static String getMD5(File file) {
        FileInputStream fileInputStream = null;
        try {
            MessageDigest MD5 = MessageDigest.getInstance("MD5");
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                MD5.update(buffer, 0, length);
            }
            return new String(Hex.encodeHex(MD5.digest()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        doPost(sendQywxTextUrl, "test@credithc.com", "test");
        doPostUpload(uploadFileUrl, "D://test.csv");
        doGet("testurl");
    }

}

results matching ""

    No results matching ""