URL读/写器类,用于向URL写数据/从URL读数据
import java.io.BufferedReader; /** 来 自 N o w J a v a . c o m - 时 代 Java **/ import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.util.Map; public class Main { public static String getURLContent(String urlStr, String input) { BufferedReader br = null; BufferedWriter bw = null;/** n o w j a v a . c o m - 时 代 Java 提 供 **/ try { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.connect(); OutputStream ous = connection.getOutputStream(); bw = new BufferedWriter(new OutputStreamWriter(ous)); bw.write(input); bw.flush(); bw.close(); printRequestHeaders(connection); InputStream ins = connection.getInputStream(); br = new BufferedReader(new InputStreamReader(ins)); StringBuffer sb = new StringBuffer(); String msg = null; while ((msg = br.readLine()) != null) { sb.append(msg); sb.append("\n"); // Append a new line } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } public static void printRequestHeaders(URLConnection connection) { Map headers = connection.getHeaderFields();