简单的java http客户端没有服务器响应 [英] Simple java http client no server response

查看:70
本文介绍了简单的java http客户端没有服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的Java http客户端,它只打印出一行服务器响应。我的问题是我得不到服务器的响应。这就是我所拥有的,即编译和运行时没有明显的错误,它只是在我键入主机名后挂起,例如'www.google.com':

I'm attempting to write a simple Java http client that simply prints out one line of the server response. My problem is that I get no response from the server. Here is what I have, which is compiling and running with no explicit errors, it just hangs after I type a hostname, e.g. 'www.google.com':

import java.io.*;
import java.net.*;

public class DNSTest {
    // Constructor
    public DNSTest() { }

    // Builds GET request, opens socket, waits for response, closes
    public static void main(String[] args) throws Exception{
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //For each hostname
        while ((line = br.readLine()) != null){ 
            //Resolve the hostname to an IP address
            InetAddress ip = InetAddress.getByName(line);

            //Open socket on ip address
            Socket socket = new Socket(ip, 80);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //Send request
            out.println("GET /index.html HTTP/1.0\n");

            //Read one line of input
            System.out.println("Response from "+line+": "+in.readLine());

        }
    }   
}

任何建议?请注意,这假设存在'index.html' - 即使这是真的,它仍然会挂起。

Any suggestions? Note that this assumes an 'index.html' exists - it still just hangs even if this is true.

推荐答案

我想我已经能够通过对代码应用一个小的更改来重现问题,所以现在它在我的机器上不起作用并且表现出与在环境中相同的行为。我刚刚将 out.println 的调用更改为更简单的 out.print ,并且看,程序挂起发出请求之后 之前最后一次 println 调用。

I think I have been able to reproduce the problem by applying a small change to the code, so now it doesn't work on my machine and exhibits the same behavior as it does in your environment. I just changed the out.println call to a simpler out.print, and lo and behold, the program hangs after sending out the request but before the last println call.

据我所知,HTTP请求需要在标题后面提供一个空行,其中行分隔符必须是 \\\\ n 几个字符。我想您的环境是这样的 println 不会发送正确的行分隔符(您可以检查 System.getProperty(line.separator的值) )来验证系统上使用的是哪个),因此服务器将请求解释为不完整,输入端什么也得不到。有些服务器非常宽容,只接受 \ n 作为行分隔符,但如果您碰巧发送 \ n 显式地和 \\\\ n 隐式地(通过 println )然后你所谓的空行包含 \ r 字符并且不再被视为空,可能会导致请求无法阻止服务器发回任何响应。

As far as I can remember, HTTP requests need to supply an empty line after the headers, where the line separator must be the \r\n couple of characters. I suppose that your environment is such that println does not send the correct line separators (you can check the value of System.getProperty("line.separator") to verify which are the ones used on your system), so the request is interpreted by the server as incomplete and you get nothing on the input side. Some servers are quite forgiving and accept just \n as line separator, but if you happen to send \n explicitly and \r\n implicitly (by means of println) then your supposedly empty line contains the \r character and is no more seen as empty, possibly faulting the request to the effect of preventing the server to send back any response.

因此,我的建议是使用 print 并明确发送正确的行分隔符。另外,你很可能需要添加一个对 flush 的调用,也许是因为输出之间有一个空行 - 我真的不知道,但没有调用 flush 我的程序仍然挂起。总而言之,以下内容应该有效:

Therefore, my advice is to use print and send the correct line separators explicitly. Also, you very probably need to add a call to flush, maybe because there's an empty line in between the output - I don't really know, but without invoking flush my program still hangs. So, to sum up, the following should work:

// send request
out.print("GET /index.html HTTP/1.0\r\n\r\n");
out.flush();
// read one line of input
System.out.println("Response from " + line + ": " + in.readLine());

至少,我可以通过这些更改确认程序在我的机器上运行。

At least, I can confirm the program works on my machine with these changes, too.

这篇关于简单的java http客户端没有服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆