import java.net.*;
import java.io.*;
public class Google {
public Boolean dlgooglepage(String Keyworld, int pagenum) {
try {
String a1 = "/tw/auction/d33856968";
String a2 = "tw.f4.page.bid.yahoo.com";
Socket s = new Socket(a2, 80);
String head = "Request " + a1 + " HTTP/1.1\n" +
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*" +
"Accept-Language: zh-tw" +
"UA-CPU: x86" +
"Accept-Encoding: gzip, deflate" +
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; InfoPath.2)" +
"Host: " + a2 + "\n" +
"Connection: Keep-Alive\n\n";
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());
out.print(head);
out.flush();
FileWriter output = new FileWriter("Http.html");
String line = "";
while ((line = in.readLine()) != null) {
output.write(line + "\n\r");
}
output.close();
out.close();
} catch (Exception e) {
System.out.println("Wrong");
return false;
}
return true;
}
}
Method 2:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.*;
import java.io.*;
public class Google {
public Boolean dlgooglepage(String Keyworld, int pagenum) {
HttpURLConnection httpConn = null;
try {
URL url = new URL("http://tw.bid.yahoo.com/tw");
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
//httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible//; MSIE 7.0//; Windows NT 6.0//; SLCC1//; .NET CLR 2.0.50727//; .NET CLR 3.0.04506//; InfoPath.2)");
InputStream in = httpConn.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
String result = null;
FileWriter output = new FileWriter("Http111.html");
while ((result = bf.readLine()) != null) {
output.write(result + "\n\r");
}
output.close();
} catch (Exception e) {
System.out.println("Wrong");
return false;
}
return true;
}
}
沒有留言:
張貼留言