package test;
創(chuàng)新互聯(lián)公司是專業(yè)的六合網(wǎng)站建設(shè)公司,六合接單;提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行六合網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
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ù)鏈接(字符串格式),生成一個(gè)URL對(duì)象
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)致這種情況的原因主要是……
只能抓取靜態(tài)的頁面源代碼,因?yàn)楹芏嗍录蜆邮绞莿?dòng)態(tài)綁定和執(zhí)行的,所以不可能獲取到執(zhí)行完后的代碼的。
public
String
getHtmlContent(String
htmlurl)
{
URL
url;
String
temp;
StringBuffer
sb
=
new
StringBuffer();
try
{
url
=
new
URL(htmlurl);
BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(url.openStream(),
"gbk"));
while
((temp
=
in.readLine())
!=
null)
{
sb.append(temp);
}
in.close();
}
catch
(final
MalformedURLException
me)
{
me.getMessage();
}
catch
(final
IOException
e)
{
e.printStackTrace();
}
return
sb.toString();
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
String urlString;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest(網(wǎng)址);
client.run();
}
public HttpTest(String urlString) {
this.urlString = urlString;
}
public void run() throws Exception {
//生成一個(gè)URL對(duì)象
URL url = new URL(urlString);
//打開URL
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//得到輸入流,即獲得了網(wǎng)頁的內(nèi)容
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
.getInputStream()));
String line;
// 讀取輸入流的數(shù)據(jù),并顯示
while ((line = reader.readLine()) != null){
System.out.println(line);
}
}
}
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
{
//讀入U(xiǎn)RL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 為增加性能存儲(chǔ)輸入流
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}