2013-04-10 53 views
2

我通过构建Java的服务器,与HORSTMANN的大的Java沿着以下工作。当我完成一个程序,“只是建立一个到主机的连接,发送一个GET命令给主机,然后从服务器接收输入,直到服务器关闭它的连接,”我决定在我自己的网站上尝试它。Java服务器的奇怪输出 - 我的网站是否被黑客攻击?

它返回的代码看起来没有what the html on my site looks like。事实上,它看起来像是完全和完全劫持我的网站的东西。当然,网站本身仍然看起来一如既往...

我真的不知道我在这里看到什么。我仔细检查了代码是否正确。是Java方面的问题,还是在我身边?

这里是Java:

import java.io.InputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.util.Scanner; 

public class WebGet { 
    public static void main(String[] args) throws IOException { 
     // Get command-line arguments 
     String host; 
     String resource; 

    if (args.length == 2) { 
     host = args[0]; 
     resource = args[1]; 
    } else { 
     System.out.println("Getting/from thelinell.com"); 
     host = "thelinell.com"; 
     resource = "/"; 
    } 

    // Open Socket 
    final int HTTP_PORT = 80; 
    Socket s = new Socket(host, HTTP_PORT); 
    // Get Streams 
    InputStream instream = s.getInputStream(); 
    OutputStream outstream = s.getOutputStream(); 
    // Turn streams into scanners and writers 
    Scanner in = new Scanner(instream); 
    PrintWriter out = new PrintWriter(outstream); 
    // Send command 
    String command = "GET " + resource + "HTTP/1.1\n" + "Host: " + host + "\n\n"; 
    out.print(command); 
    out.flush(); 
    // Read server response 
    while (in.hasNextLine()) { 
     String input = in.nextLine(); 
     System.out.println(input); 
    } 

    // Close the socket 
    s.close(); 
} 
} 

现在,正在恢复看起来像一堆广告和相当长的代码。为简洁起见,以下是它给我的pastebin。如果要求,我会在这里添加它。

回答

3

你需要一个空间之间的资源URI和HTTP版本的片段:

String command = "GET " + resource + "HTTP/1.1\n" ... 

应该是:

String command = "GET " + resource + " HTTP/1.1\n" ... 

因为它是现在,你的要求是这样的:

GET /HTTP/1.1
主持人:thelinell.com

尽管对HTTP 1.1无效,但它仍然被您的虚拟主机提供商拦截(可能为Simple-Request),然后又回到那个(令人发指的)横幅广告集合。

+1

实际上,这是一个有效的请求,它是[Simple-Request](http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#Request)(至少是第一行,第二行被忽略),它将尝试从服务器的默认域中获取'/ HTTP/1.1'。 – mata 2013-04-11 00:14:55

+0

我叫自己检查。你是对的! – Linell 2013-04-11 00:15:50

+0

@mata - 在Simple-Request上打出良好的电话。 – Perception 2013-04-11 00:28:39

相关问题