Java 获取带有套接字的网页
我目前正在学习socket编程,遇到了一个需要帮助的问题。我试图做的是编写一个小Java类,它将连接到web主机,下载默认页面,然后断开与主机的连接。我知道使用URLConnection来实现这一点更简单,但我正在尝试学习套接字类。我已经成功地连接到web服务器,但是我很难进入页面。这就是我目前正在工作(和未工作)的内容:Java 获取带有套接字的网页,java,sockets,Java,Sockets,我目前正在学习socket编程,遇到了一个需要帮助的问题。我试图做的是编写一个小Java类,它将连接到web主机,下载默认页面,然后断开与主机的连接。我知道使用URLConnection来实现这一点更简单,但我正在尝试学习套接字类。我已经成功地连接到web服务器,但是我很难进入页面。这就是我目前正在工作(和未工作)的内容: 我只想让这个类说出“curl”、“lynx-dump”或“wget”等可能输出的内容。非常感谢您提供的任何帮助。您需要向套接字的输出流写入一些内容。Web服务器在发送任何内容
我只想让这个类说出“curl”、“lynx-dump”或“wget”等可能输出的内容。非常感谢您提供的任何帮助。您需要向套接字的输出流写入一些内容。Web服务器在发送任何内容之前等待客户端的请求:写入“GET”将要求服务器返回默认页面
您的代码没有写入任何内容,因此服务器将永远等待。您的想法是正确的,但您没有提交HTTP请求。发送:
GET/HTTP/1.1\r\nHost:嗯,它会在几分钟到几秒钟后超时,但是是的。当然。“永远”在这里有点比喻性:)作为旁白,我强烈建议您不要捕捉NullPointerException
,除非您确实确定自己在做什么NullPointerException
s通常表示存在重大错误,让程序barf具有堆栈跟踪通常非常有用。尝试了以下方法,但没有任何乐趣。我做错了什么?//向服务器发送请求请尝试{theOutput=new BufferedWriter(new OutputStreamWriter(theSocket.getOutputStream());theOutput.write(httpHeader);}catch(IOException ioe){System.out.println(“*”+“无法写入套接字”);}查看我上面的编辑。你有什么问题?httpHeader的内容是什么?我只是没有从服务器上得到任何东西。我在“localhost”上运行了这个程序,并使用Wireshark查看了事务,因此我知道正在发生一些事情,但服务器仍然没有输出。另外,字符串httpHeader=“GET/HTTP/1.1\r\n主机:“+theHost+”\r\n\r\n”;愚蠢的问题-你有在本地主机上运行的Web服务器吗?如果有的话,Web服务器日志会说什么?您是否尝试刷新输出流?除非调用flush
,否则BufferedWriter不一定会写入底层流。
import java.io.*;
import java.net.*;
import java.lang.IllegalArgumentException;
public class SocketsFun{
public static void main(String[] myArgs){
// Set some variables
String theServer = null;
String theLine = null;
int thePort = 0;
Socket theSocket = null;
boolean exit = false;
boolean socketCheck = false;
BufferedReader theInput = null;
// Grab the server and port number
try{
theServer = myArgs[0];
thePort = Integer.parseInt(myArgs[1]);
System.out.println("Opening a connection to " + theServer + " on port " + thePort);
} catch(ArrayIndexOutOfBoundsException aioobe){
System.out.println("usage: SocketsFun host port");
exit = true;
} catch(NumberFormatException nfe) {
System.out.println("usage: SocketsFun host port");
exit = true;
}
if(!exit){
// Open the socket
try{
theSocket = new Socket(theServer, thePort);
} catch(UnknownHostException uhe){
System.out.println("* " + theServer + " does not exist");
} catch(IOException ioe){
System.out.println("* " + "Connection Refused");
} catch(IllegalArgumentException iae){
System.out.println("* " + thePort + " Not A Valid TCP/UDP Port.");
}
// Print out some stuff
try{
System.out.println("Connected Socket: " + theSocket.toString());
} catch(Exception e){
System.out.println("* " + "No Open Socket");
}
try{
theInput = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
while ((theLine = theInput.readLine()) != null){
System.out.println(theLine);
}
theInput.close();
} catch(IOException ioe){
System.out.println("* " + "No Data To Read");
} catch(NullPointerException npe){
System.out.println("* " + "No Data To Read");
}
// Close the socket
try{
socketCheck = theSocket.isConnected();
} catch(NullPointerException npe){
System.out.println("* " + "No Socket To Close");
}
}
}
}
[METHOD] [PATH] HTTP/1.1 [CRLF]
Host: [HOSTNAME] [CRLF]
OTHER: HEADERS [CRLF]
[CRLF]
GET / HTTP/1.1[ENTER]
Host: google.com[ENTER]
[ENTER]
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 09 Dec 2010 00:03:39 GMT
Expires: Sat, 08 Jan 2011 00:03:39 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
<HTML&<HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>