package test; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSON; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.assertj.core.util.Files; import org.junit.Assert; import org.junit.Test; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** * @desc: 接口測試 * @author: zengxc * @date: 2018/3/28 */ public class HttpTest { private static OkHttpClient httpClient = new OkHttpClient.Builder() .readTimeout(2, TimeUnit.SECONDS) .writeTimeout(2, TimeUnit.SECONDS) .connectTimeout(3, TimeUnit.SECONDS) .build(); private Map<String, Object> map = new ConcurrentHashMap<String, Object>(); private static String URL_FACTOR = FileUtil.readString(new File("D:\\WorkSpace\\url.txt"), "utf-8"); private final static String AUTH_VALULE = FileUtil.readString(new File("D:\\WorkSpace\\auth.txt"), "utf-8"); private final static List<String> IMEIS = Files.linesOf(new File("D:\\WorkSpace\\imei.txt"), "utf-8"); @Test public void test01() throws IOException { for(String imei : this.IMEIS){ getResponseDate(imei, this.map); } System.out.println(map.size()); } public void getResponseDate(String imei, Map<String, Object> map) throws IOException { String url = URL_FACTOR.replace("{imei}", URLEncoder.encode(imei, "UTF-8")); Request request = new Request.Builder() .url(url) .addHeader("Authorization", AUTH_VALULE).get() .build(); Response response = httpClient.newCall(request).execute(); String string = response.body().string(); Map<String, Object> data = JSON.parseObject(string).getObject("data", Map.class); map.putAll(data); } @Test public void test02(){ List<String> list = Files.linesOf(new File("D:\\WorkSpace\\imei.txt"), "utf-8"); Assert.assertNotNull("data can't for null", list); } }