国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 抓取網(wǎng)頁內(nèi)容實(shí)現(xiàn)代碼

2019-11-26 15:38:57
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

package test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.Authenticator;

import java.net.HttpURLConnection;

import java.net.PasswordAuthentication;

import java.net.URL;

import java.net.URLConnection;

import java.util.Properties;

 

public class URLTest {

 // 一個(gè)public方法,返回字符串,錯(cuò)誤則返回"error open url"

 public static String getContent(String strUrl) {

  try {

   URL url = new URL(strUrl);

   BufferedReader br = new BufferedReader(new InputStreamReader(url

     .openStream()));

   String s = "";

   StringBuffer sb = new StringBuffer("");

   while ((s = br.readLine()) != null) {

    sb.append(s + "/r/n");

   }

   br.close();

   return sb.toString();

  } catch (Exception e) {

   return "error open url:" + strUrl;

  }

 }

 

 public static void initProxy(String host, int port, final String username,

   final String password) {

  Authenticator.setDefault(new Authenticator() {

   protected PasswordAuthentication getPasswordAuthentication() {

    return new PasswordAuthentication(username,

      new String(password).toCharArray());

   }

  });

  System.setProperty("http.proxyType", "4");

  System.setProperty("http.proxyPort", Integer.toString(port));

  System.setProperty("http.proxyHost", host);

  System.setProperty("http.proxySet", "true");

 }

 

 public static void main(String[] args) throws IOException {

   String url = "http://www.survivalescaperooms.com";

   String proxy = "http://192.168.22.81";

   int port = 80;

   String username = "username";

   String password = "password";

   String curLine = "";

   String content = "";

   URL server = new URL(url);

   initProxy(proxy, port, username, password);

   HttpURLConnection connection = (HttpURLConnection) server

   .openConnection();

   connection.connect();

   InputStream is = connection.getInputStream();

   BufferedReader reader = new BufferedReader(new

   InputStreamReader(is));

   while ((curLine = reader.readLine()) != null) {

   content = content + curLine+ "/r/n";

   }

   System.out.println("content= " + content);

   is.close();

   System.out.println(getContent(url));

 }

}       

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石阡县| 宝兴县| 深水埗区| 龙里县| 郎溪县| 芷江| 城市| 安阳县| 兰州市| 耒阳市| 沈丘县| 高邑县| 象山县| 望都县| 长兴县| 东平县| 齐齐哈尔市| 广东省| 望谟县| 全椒县| 太仆寺旗| 财经| 贺州市| 常熟市| 庆阳市| 蒙阴县| 荔浦县| 辽宁省| 慈利县| 和政县| 古交市| 临颍县| 浏阳市| 龙门县| 老河口市| 河间市| 津南区| 南丰县| 灌云县| 遂昌县| 平邑县|