图书ISBN查询 Java 参考代码


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 图书ISBN查询 示例代码
 **/
public class PostTest {

	public static void main(String[] args) throws Exception {
		String domain = "http://api.data5u.com";
		String api = "/book/isbn";
		String params = "appsec=&isbn=";   // 参数值请自行补全
		String method = "POST";
		
    	HttpURLConnection connection = null;
    	URL link = new URL(domain + api);
    
		connection = (HttpURLConnection)link.openConnection();
		
		connection.setRequestMethod(method); 
    	connection.setDoOutput(true);
    	connection.setUseCaches(false);
    	connection.setReadTimeout(10000);
    	
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));
        out.write(params);
        out.flush();
        out.close();   
    	
        String line = null;
        StringBuilder html = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        while((line = reader.readLine()) != null){
        	html.append(line);
        }
        
        try {
			if (reader != null) {
				reader.close();
			}
		} catch (Exception e) {
		}
        
        System.out.println(html.toString());
	}

}