博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java URL学习
阅读量:6767 次
发布时间:2019-06-26

本文共 941 字,大约阅读时间需要 3 分钟。

hot3.png

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"));	}}

转载于:https://my.oschina.net/u/138995/blog/304911

你可能感兴趣的文章