1、Socket读取
String strServer=http://www.google.cn;//这里同样可以用ip来访问:203.208.35.100
String strPage="/language_tools?hl=zh-CN";
try {
String hostname = strServer;
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("GET " + strPage + " HTTP/1.0\r\n");
wr.write("HOST:" + strServer + "\r\n");
wr.write("\r\n");
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
System.out.println(e.toString());
}
2、HttpClient方式
HttpClient client=new HttpClient();
GetMethod method=new GetMethod("http://www.baidu.com/");
int status=client.executeMethod(method);
if(status==HttpStatus.SC_OK){
//读取内容
byte[] responseBody = method.getResponseBody();
//处理内容
System.out.println(new String(responseBody));
System.out.println("文件名称:"+method.getPath());
}
3、HttpURLConnection方式
String sCurrentLine;
String sTotalString;
sCurrentLine="";
sTotalString="";
java.io.InputStream l_urlStream;
java.net.URL l_url = new java.net.URL("http://www.sina.com/");
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString+=sCurrentLine;
}
HttpClient和GetMethod都是来自apache的包
String strServer=http://www.google.cn;//这里同样可以用ip来访问:203.208.35.100
String strPage="/language_tools?hl=zh-CN";
try {
String hostname = strServer;
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("GET " + strPage + " HTTP/1.0\r\n");
wr.write("HOST:" + strServer + "\r\n");
wr.write("\r\n");
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
System.out.println(e.toString());
}
2、HttpClient方式
HttpClient client=new HttpClient();
GetMethod method=new GetMethod("http://www.baidu.com/");
int status=client.executeMethod(method);
if(status==HttpStatus.SC_OK){
//读取内容
byte[] responseBody = method.getResponseBody();
//处理内容
System.out.println(new String(responseBody));
System.out.println("文件名称:"+method.getPath());
}
3、HttpURLConnection方式
String sCurrentLine;
String sTotalString;
sCurrentLine="";
sTotalString="";
java.io.InputStream l_urlStream;
java.net.URL l_url = new java.net.URL("http://www.sina.com/");
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString+=sCurrentLine;
}
HttpClient和GetMethod都是来自apache的包