Java联网技术之一HTTP
生活随笔
收集整理的這篇文章主要介紹了
Java联网技术之一HTTP
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
學(xué)到Java的聯(lián)網(wǎng)技術(shù),這里首先來看看關(guān)于URl,
要從網(wǎng)上獲得內(nèi)容,
需要實(shí)現(xiàn)下面的4步,
1.創(chuàng)建一個表示資源的網(wǎng)絡(luò)地址的URL對象,
2.創(chuàng)建一個HttpURLConnection 連接對象
3.從得到的連接對象里面獲取內(nèi)容getContent()
4.輸出得到的內(nèi)容
下面是代碼:
package com.founder.sun;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;public class Demo04 {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubURL page = new URL("http://www.baidu.com");HttpURLConnection conn = (HttpURLConnection) page.openConnection(); InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());BufferedReader buff = new BufferedReader(in);String line;do {line = buff.readLine();System.out.println(line);System.out.println("/n");} while (line != null); System.out.println("Over______________");}}該代碼是向百度發(fā)出請求,的回來一個返回的內(nèi)容。
如圖:
?
可以和百度的源代碼進(jìn)行比較:
?
轉(zhuǎn)載于:https://www.cnblogs.com/sunxun/p/5345677.html
總結(jié)
以上是生活随笔為你收集整理的Java联网技术之一HTTP的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server 2008 R2 开
- 下一篇: Java递归算法——二分查找