package com.wyl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class APItest {
public static void main(String[] args) throws IOException {
String APIKEY = "开发者注册帐号,激活之后即可获得";
String INFO = URLEncoder.encode("讲个笑话", "utf-8");
String getURL = "http://www.tuling123.com/openapi/api?key=" + "e59219b3f450256c2a3da7157ae651f2" + "&info=" + INFO;
URL getUrl = new URL(getURL);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 断开连接
connection.disconnect();
System.out.println(sb);
}
}