標籤:open stringbu lead clipboard board ext gen boa view
[java] view plain copy
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
-
- import sun.net.www.protocol.http.HttpURLConnection;
-
- public class UrlTest {
- public static void main(String[] args) throws Exception {
- //建立串連的url
- URL url = new URL("http://openapi.sparta.html5.qq.com");
- //建立串連,注意使用的包為sun.net.www.protocol.http.HttpURLConnection
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- //連線逾時時間:16秒
- conn.setConnectTimeout(16000);
- //返回資料逾時時間
- conn.setReadTimeout(16000);
- //使用流讀取返回的資料
- InputStream is = conn.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));
- String line = null;
- StringBuffer sb = new StringBuffer();
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- System.out.println(sb.toString());
- }
- }
使用net包發送http請求