java api调用示例

package com.zj.api.util.interfaceDevTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.fastjson.JSONObject;

public class GetSubjectListTest1 {

	/***
	 * 获取科目信息
	 * @param args
	 */
	public static void main(String[] args) {
        String apiUrl = "http://localhost:8888/v1/subject/list";
		String developerId = "2017042528467199";
		String accessSecret = "2171fd0928df4f9792f8cce3a9936d5a";
		
		// 创建参数表
		Map paramMap = new HashMap();
		paramMap.put("stage",3);
		
		paramMap.put("developerId", developerId);
		long currentDate = new Date().getTime();
		paramMap.put("timestamp", currentDate);
		
		// 对参数名进行字典排序
		String[] keyArray = paramMap.keySet().toArray(new String[0]);
		Arrays.sort(keyArray);
		// 拼接有序的参数名-值串
		StringBuilder needEncryptString = new StringBuilder();
		StringBuilder paramBuffer = new StringBuilder();
		for (String key : keyArray) {
			paramBuffer.append(key).append("=")
					.append(paramMap.get(key) == null ? "" : paramMap.get(key))
					.append("&");
			if (key == null || paramMap.get(key) == null) {
				continue;
			}
			needEncryptString.append(key).append("=").append(paramMap.get(key)).append("&");
		}

		StringBuilder URL = new StringBuilder();
		URL.append(apiUrl).append("?").append(paramBuffer);

		// 拼接上开发者Secret Key
		needEncryptString.append("accessSecret").append("=").append(accessSecret);

		System.out.println(needEncryptString);
		String encryptString = DigestUtils.sha1Hex(needEncryptString.toString());

		// 签名示例
		URL.append("signature=").append(encryptString);
		System.out.println(URL);

		// 接收API调用结果的json对象
		try {
			System.out.println(readContentFromGet(URL.toString()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}

	public static String readContentFromGet(String URL) throws IOException {

		// 定义返回API调用结果的json对象
		JSONObject jsonResult = new JSONObject();

		// 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码
		// String getURL = URLEncoder.encode(URL, "utf-8");
		URL = URL.replace(" ", "%20");// 为了防止URL中空格报错,用%20取代空格

		URL getUrl = new URL(URL);

		// 根据拼凑的URL,打开连接,URL.openConnection()函数会根据
		// URL的类型,返回不同的URLConnection子类的对象,在这里我们的URL是一个http,因此它实际上返回的是HttpURLConnection
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();

		// 建立与服务器的连接,并未发送数据
		connection.connect();

		// 发送数据到服务器并使用Reader读取返回的数据
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String lines;
		StringBuffer sb = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			sb.append(lines);
		}
		String rs = sb.toString();
		reader.close();
		// 断开连接
		connection.disconnect();
		return rs;
	}

}

你可能感兴趣的:(java api调用示例)