我想通过Socket链接到HTTP服务器,请求获取一个页面的内容。本地电脑上开了TOMCAT,端口已改为80。但利用socket获取输入流的时候发生错误,不知道为什么。import java.io.*;
import java.net.*;public class GetDataFromSocket {
Socket socket = null;
BufferedWriter writer = null;
BufferedReader reader = null;
String hostname = "localhost";
String page = "/StrutsLogin/index.jsp"; public static void main(String[] args) {
new GetDataFromSocket().action();
}
public void action() {
conServer();
getWriter();
sendData(page);
getReader();
getData();
}
public void conServer() {
int port = 80;
InetAddress addr = null;
try {
addr = InetAddress.getByName(hostname);
System.out.println(addr);
socket = new Socket(addr, port);
System.out.println("conServer");
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void getWriter() {
try {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream (), "UTF8"));
System.out.println("getWriter");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getReader() {
try {
InputStream is = socket.getInputStream();
System.out.println(is);
reader = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendData(String data) {
try {
writer.write("GET " + data + " HTTP/1.0\r\n");
writer.write("Host:" + hostname + "\r\n");
writer.write("\r\n");
writer.flush();
System.out.println("sendData");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null) {
writer.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
public void getData() {
String line = null;
try {
line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} }}
import java.net.*;public class GetDataFromSocket {
Socket socket = null;
BufferedWriter writer = null;
BufferedReader reader = null;
String hostname = "localhost";
String page = "/StrutsLogin/index.jsp"; public static void main(String[] args) {
new GetDataFromSocket().action();
}
public void action() {
conServer();
getWriter();
sendData(page);
getReader();
getData();
}
public void conServer() {
int port = 80;
InetAddress addr = null;
try {
addr = InetAddress.getByName(hostname);
System.out.println(addr);
socket = new Socket(addr, port);
System.out.println("conServer");
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void getWriter() {
try {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream (), "UTF8"));
System.out.println("getWriter");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getReader() {
try {
InputStream is = socket.getInputStream();
System.out.println(is);
reader = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendData(String data) {
try {
writer.write("GET " + data + " HTTP/1.0\r\n");
writer.write("Host:" + hostname + "\r\n");
writer.write("\r\n");
writer.flush();
System.out.println("sendData");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null) {
writer.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
public void getData() {
String line = null;
try {
line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} }}
需要模拟出http协议的通讯格式
这种抓取网页内容的方法还是第一次看到。楼住可以获取创意奖了
兄弟可以用
URL m_URL = new URL("抓取网页的地址");
HttpConnection con = m_URL.openConnection();
OutputStream output = con.getOutputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte bytes[] = new byte[1024];
int = -1;
while((=output.write(bytes)))
{
bos.write(bytes,0,);
}
String content = bos.toString(); // 网页内容
HttpConnection con = m_URL.openConnection();
OutputStream output = con.getOutputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte bytes[] = new byte[1024];
int = -1;
while((=output.write(bytes)))
{
bos.write(bytes,0,);
}
String content = bos.toString(); // 网页内容