采用HttpURLConnection方式调用第三方接口,接口的返回值经过unicode编码处理

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Java_WGE/article/details/79022920

在接口的调用过程中,第三方接口的返回值使用unicode处理了,调用过程中一直报错

改成下面这样就可以了

public class TestStatic {
	public static void main(String[] args) throws Exception {
		getDatas();
	}
	public static void getDatas(){
		HttpURLConnection conn = null;
		try {
			String INFORMATION_URL = "http://250.250.250:8080/datasnap/rest/TInterFace/getoverdayreaderinfor";
			conn = getConnection(INFORMATION_URL, "GET");
			conn.setConnectTimeout(30000);  
			conn.setReadTimeout(30000);  
			conn.connect();
			String resp = ""; 
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
				String next = null;
				while ((next = reader.readLine()) != null) {
					resp += next;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			conn.disconnect();
			System.out.println(resp);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static HttpURLConnection getConnection(String urlString,String verb) {
		HttpURLConnection conn = null;
		try {
			URL url = new URL(urlString);
			conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod(verb);
			conn.setDoInput(true);
			conn.setDoOutput(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
}


展开阅读全文

没有更多推荐了,返回首页