用java 实现网络爬虫socket url 使用一个就行了吗?还要向服务器发get 命令吗?
这样做能直接读文件吗?
package cn.edu.suda.http.connection;import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;import cn.edu.suda.util.TimedSocket;/**
*
* @author yangshoujian
*
*/
public class HttpConnection { /** the TCP socket to use */
private Socket socket = null;
/**
* create a TCP Connection
* @param address
* @param port
* @param timeout
* @return
* @throws IOException
*/
public static HttpConnection createConnection(InetAddress address, int port,int timeout)
throws IOException
{
HttpConnection connection = new HttpConnection();
try {
connection.socket = TimedSocket.getSocket(address, port, timeout);
connection.socket.setSoTimeout(timeout);
} catch (InterruptedIOException e) {
throw new IOException("timeout during connect: "+e.getMessage());
}
return connection;
}
/**
* @return InputStream 对象。
* @throws IOException
*/
public InputStream getInputStream() throws IOException {
if (socket == null) throw new IOException("not conected");
return socket.getInputStream();
}
/**
* @return OutputStream 对象。
* @throws IOException
*/
public OutputStream getOutputStream() throws IOException {
if (socket == null) throw new IOException("not conected");
return socket.getOutputStream();
}
/**
* 关闭Socket 连接。
*/
public void close() {
try {
socket.close();
} catch (IOException e) {
// do not throw an I/O error on close
}
}
/**
* Create a new HttpConnection
*/
protected HttpConnection() {
}
public HttpConnection(Socket socket) {
this.socket=socket;
}
}
这样做能直接读文件吗?
package cn.edu.suda.http.connection;import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;import cn.edu.suda.util.TimedSocket;/**
*
* @author yangshoujian
*
*/
public class HttpConnection { /** the TCP socket to use */
private Socket socket = null;
/**
* create a TCP Connection
* @param address
* @param port
* @param timeout
* @return
* @throws IOException
*/
public static HttpConnection createConnection(InetAddress address, int port,int timeout)
throws IOException
{
HttpConnection connection = new HttpConnection();
try {
connection.socket = TimedSocket.getSocket(address, port, timeout);
connection.socket.setSoTimeout(timeout);
} catch (InterruptedIOException e) {
throw new IOException("timeout during connect: "+e.getMessage());
}
return connection;
}
/**
* @return InputStream 对象。
* @throws IOException
*/
public InputStream getInputStream() throws IOException {
if (socket == null) throw new IOException("not conected");
return socket.getInputStream();
}
/**
* @return OutputStream 对象。
* @throws IOException
*/
public OutputStream getOutputStream() throws IOException {
if (socket == null) throw new IOException("not conected");
return socket.getOutputStream();
}
/**
* 关闭Socket 连接。
*/
public void close() {
try {
socket.close();
} catch (IOException e) {
// do not throw an I/O error on close
}
}
/**
* Create a new HttpConnection
*/
protected HttpConnection() {
}
public HttpConnection(Socket socket) {
this.socket=socket;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货