import java.io.*;
import java.net.*;public class Spider implements Runnable{
HttpURLConnection huc;
InputStream is;
BufferedReader reader;
String url;
public Spider(){
try {
url="http://localhost:8080/a.html";
} catch (Exception e) {
e.printStackTrace();
}
try {
huc=(HttpURLConnection)new URL(url).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
} public void run() {
try {
huc.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
huc.setUseCaches(true);
huc.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
is=huc.getInputStream();
reader=new BufferedReader(new InputStreamReader(is,huc.getContentType().equals("text-html; charset=gb2312")?"gb2312":"UTF-8"));
StringBuffer temp=new StringBuffer();
String str;
while((str=reader.readLine())!=null){
temp.append(str+"\n");
}
System.out.println(new String(temp));
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
is.close();
huc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String [] args){
Spider sp=new Spider();
sp.run();
}
}
我读出的那个url下的html顺序乱了,请求各位朋友提供解决方案
import java.net.*;public class Spider implements Runnable{
HttpURLConnection huc;
InputStream is;
BufferedReader reader;
String url;
public Spider(){
try {
url="http://localhost:8080/a.html";
} catch (Exception e) {
e.printStackTrace();
}
try {
huc=(HttpURLConnection)new URL(url).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
} public void run() {
try {
huc.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
huc.setUseCaches(true);
huc.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
is=huc.getInputStream();
reader=new BufferedReader(new InputStreamReader(is,huc.getContentType().equals("text-html; charset=gb2312")?"gb2312":"UTF-8"));
StringBuffer temp=new StringBuffer();
String str;
while((str=reader.readLine())!=null){
temp.append(str+"\n");
}
System.out.println(new String(temp));
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
is.close();
huc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String [] args){
Spider sp=new Spider();
sp.run();
}
}
我读出的那个url下的html顺序乱了,请求各位朋友提供解决方案
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货