java查看網(wǎng)頁源代碼 怎么在網(wǎng)頁查看源代碼

java中如何根據(jù)一個網(wǎng)址獲得該網(wǎng)頁的源代碼?

package test;

成都創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、臨縣網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為臨縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpTest {

private String u;

private String encoding;

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

HttpTest client = new HttpTest("", "UTF-8");

client.run();

}

public HttpTest(String u, String encoding) {

this.u = u;

this.encoding = encoding;

}

public void run() throws Exception {

URL url = new URL(u);// 根據(jù)鏈接(字符串格式),生成一個URL對象

HttpURLConnection urlConnection = (HttpURLConnection) url

.openConnection();// 打開URL

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網(wǎng)頁的內(nèi)容

String line; // 讀取輸入流的數(shù)據(jù),并顯示

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

System.out.println(line);

}

}

}

根據(jù)具體問題類型,進(jìn)行步驟拆解/原因原理分析/內(nèi)容拓展等。

具體步驟如下:/導(dǎo)致這種情況的原因主要是……

java源代碼怎么打開

源代碼默認(rèn)是打不開的,可以使用反編譯工具,進(jìn)行逆向解析才能看到源代碼。

eclipse這個開發(fā)工具,默認(rèn)有反編譯的插件,在查看的類,按住ctrl點(diǎn)擊鼠標(biāo)左鍵即可查看源代碼。

Java訪問指定URL并獲取網(wǎng)頁源代碼

1.編寫useSourceViewer 類的基本框架,該類僅包括無返回值的main ()方法,該方法從參數(shù)中獲取URL,通過輸入緩沖和輸出緩沖將該URL 原碼輸出。

2.編寫useSourceViewer 類,代碼如下:

import java.net.*;

import java.io.*;

public class useSourceViewer

{

public static void main (String[] args)

{

if (args.length 0)

{

try

{

//讀入URL

URL u = new URL(args[0]);

InputStream in = u.openStream( );

// 為增加性能存儲輸入流

in = new BufferedInputStream(in);

// 將輸入流連接到閱讀器

Reader r = new InputStreamReader(in);

int c;

while ((c = r.read( )) != -1)

{

System.out.print((char) c);

}

Object o = u.getContent( );

System.out.println("I got a " + o.getClass().getName( ));

}

catch (MalformedURLException e)

{

System.err.println(args[0] + " is not a parseable URL");

}

catch (IOException e)

{

System.err.println(e);

}

} // end if

} // end main

} // end SourceViewer}

java程序讀取一個url頁面的源代碼

傳入一個url,返回源代碼; public static String getHTML(String url){// 獲取指定URL的網(wǎng)頁,返回網(wǎng)頁內(nèi)容的字符串,然后將此字符串存到文件即可 try { URL newUrl = new URL(url); URLConnection connect = newUrl.openConnection(); connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); DataInputStream dis = new DataInputStream(connect.getInputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8")); String html = ""; String readLine = null; while((readLine = in.readLine()) != null) { html = html + readLine; } in.close(); return html; }catch (MalformedURLException me){ System.out.println("MalformedURLException" + me); }catch (IOException ioe){ System.out.println("ioeException" + ioe); } return null; }

本文標(biāo)題:java查看網(wǎng)頁源代碼 怎么在網(wǎng)頁查看源代碼
文章鏈接:http://muchs.cn/article22/dosdicc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作、做網(wǎng)站網(wǎng)站改版、用戶體驗服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司