자바 URL 학습

988 단어
import java.io.UnsupportedEncodingException;
import java.net.URL;

public class UrlTest {
	public static String getReturnData(String urlString)
			throws UnsupportedEncodingException {
		String res = "";
		try {
			URL url = new URL(urlString);
			java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url
					.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("GET");
			java.io.BufferedReader in = new java.io.BufferedReader(
					new java.io.InputStreamReader(conn.getInputStream(),
							"utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				res += line;
			}
			in.close();
		} catch (Exception e) {
			System.out.println("error in wapaction,and e is " + e.getMessage());
		}
		// System.out.println(res);
		return res;
	}
	
	public static void main(String args[]) throws UnsupportedEncodingException{
		System.out.println(getReturnData("http://www.baidu.com"));
	}
}

좋은 웹페이지 즐겨찾기