日期:2014-05-17 浏览次数:20678 次
public class URLT { public static void main(String[] args) throws IOException { String strURL = "http://www.ip.cn/getip2.php?action=queryip&ip_url=218.56.35.18"; System.out.println(getURLContents(strURL)); } public static String getURLContents(String strURL) { StringBuffer sb = new StringBuffer(); try { URL url = new URL(strURL); URLConnection connection = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) connection; System .setProperty("sun.net.client.defaultConnectTimeout", "150000"); System.setProperty("sun.net.client.defaultReadTimeout", "150000"); httpConn.setRequestMethod("GET");// 设置请求为POST方法 connection.setDoOutput(true);// 可以输出 InputStreamReader isr = new InputStreamReader(httpConn .getInputStream(), "GBK"); BufferedReader br = new BufferedReader(isr); String temp; while ((temp = br.readLine()) != null) { temp = temp.trim(); if (temp != null && temp.length() > 0) { sb.append(temp); } } br.close(); isr.close(); } catch (Exception e) { System.out.println("Error 1" + e.getMessage()); } return sb.toString(); } }