首先感谢所有在这里回答问题的人。我将此论坛用作Java圣经。这是一个家庭作业问题,这是作业:
用 Java 编写一个程序,使用套接字连接到端口 80 上的 Web 服务器,使用 HTTP 协议的 GET 请求网页,并显示生成的 HTML
不知道我这样做是否正确。我对java的了解非常有限。其中大部分来自我经历过的教程。任何网站链接将不胜感激。
这是我的错误信息:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from java.net.Socket to Socket
The method getInputStream() is undefined for the type Socket
这是我的代码:
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String[] args) throws Exception
{
Server SERVER = new Server();
SERVER.run();
}
public void run() throws Exception
{
ServerSocket one = new ServerSocket(80);
//these are the two lines of code it is warning about
Socket myskt = one.accept();
InputStreamReader IR = new InputStreamReader(myskt.getInputStream());
//end of warnings
BufferedReader BR = new BufferedReader(IR);
String message = BR.readLine();
System.out.println(message);
if (message != null)
{
PrintStream PS = new PrintStream(System.out);
PS.println("Message Received");
}
URL website = new URL("www.dogs.com");
URLConnection yc = website.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in .readLine()) != null)
System.out.println(inputLine);
one.close();
}
// TODO Auto-generated method stub
}