OneYX
9/7/2017 - 9:52 AM

ModelTest.java

package io.test.model;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

public class ModelTest {
	
	private static final String urlStr = "https://api.srv.jpush.cn/v1/stat/apps/e97e68f824a5cb5ac7de8a65/data/profiles-area?apkver=&channel=&end=20170906&platform=&start=20170830";
	
	private static final String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MDUyOTE2MjkwMDUsImp0aSI6ImU5ZDJlNDdjMTYyZTRmN2FhYWEzYzM4NGZmOTE3YmMzIiwibmFtZSI6ImRvbmdkb25nd2VkZGluZzIwMTciLCJzdWIiOiJKUHVzaCIsInRpbWVzIjoxLCJ1aWQiOjE5OTIxNn0.Z5dHVMjoCnahKIKZC93EkJUGFBRohIOaxOy84P_DZ74";

	public static void main(String[] args) throws Exception {
		URL url = new URL(urlStr);
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//		connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestMethod("GET");
		connection.setRequestProperty("authorization", token);
		connection.connect();
		int responseCode = connection.getResponseCode();
		InputStream in;
		if (responseCode == 200) {
			in = connection.getInputStream();
		} else {
			in = connection.getErrorStream();
		}
		BufferedReader r  = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));

		StringBuilder sb = new StringBuilder();
		String line;
		while ((line = r.readLine()) != null) {
		    sb.append(line);
		}
		
		System.out.println("\nSending 'GET' request to URL : " + url);
		System.out.println("Response Code : " + responseCode);
		System.out.println("Response Message : " + connection.getResponseMessage());
		System.out.println("Response Content : " + sb.toString());
		System.out.println("Response HeaderFields : " + connection.getHeaderFields());
		
		in.close();
	}

}