V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Lullaby
V2EX  ›  Java

关于 http 代理请求返回 Unexpected end of file from server?

  •  
  •   Lullaby · 2015-04-10 16:50:10 +08:00 · 29776 次点击
    这是一个创建于 3541 天前的主题,其中的信息可能已经有所发展或是发生改变。

    测试代码如下
    public static void proxy() throws IOException {
    URL url = new URL("http://www.test.com/");
    InetSocketAddress address = new InetSocketAddress("x.x.x.x", 8888);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
    Authenticator.setDefault(new MyAuthenticator(new String("root".getBytes("UTF-8")), new String("password".getBytes("UTF-8"))));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
    InputStreamReader in = null;
    BufferedReader reader = null;
    try {
    in = new InputStreamReader(connection.getInputStream());
    reader = new BufferedReader(in);
    StringBuffer buffer = new StringBuffer();
    String line;
    while ((line = reader.readLine()) != null) {
    buffer.append(line);
    }
    System.out.println(buffer);
    } finally {
    if (in != null) {
    in.close();
    }
    if (reader != null) {
    reader.close();
    }
    }
    }
    如果不使用代理,一切正常

    求科普

    7 条回复    2015-04-11 14:57:21 +08:00
    Lullaby
        2
    Lullaby  
    OP
       2015-04-10 18:21:07 +08:00
    @anexplore url是我改过的,换其他的也都一样;有可能是代理服务器的问题
    zhicheng
        3
    zhicheng  
       2015-04-10 18:36:45 +08:00 via Android
    一般情况是,服务器设置了 content-length 但在客户端接收到 content-length 的 body 之前服务器或代理就把连接 close 掉了。你要检查一下,服务器和代理是不是有请求大小限制或请求时间限制。
    Lullaby
        4
    Lullaby  
    OP
       2015-04-10 19:02:22 +08:00
    代理服务器设在日本,开了ss代理,开放了8888端口接收代理请求,现在用httpclient设置代理过去,老是报The target server failed to respond
    x.x.x.x failed to respond
    Lullaby
        5
    Lullaby  
    OP
       2015-04-10 19:03:04 +08:00
    @zhicheng 不知道是不是你说的那样 回去研究下
    anexplore
        6
    anexplore  
       2015-04-10 19:19:30 +08:00
    @Lullaby 这是stackoverflow上一个问题,希望可以有帮助
    "Unexpected end of file" implies that the remote server accepted and closed the connection without sending a response. It's possible that the remote system is too busy to handle the request, or that there's a network bug that randomly drops connections.

    With the information available it's impossible to say what's going wrong. If you have access to the servers in question you can use packet sniffing tools to find what exactly is sent and received, and look at logs to of the server process to see if there are any error messages.

    http://stackoverflow.com/questions/19824339/java-simple-code-java-net-socketexception-unexpected-end-of-file-from-server
    Lullaby
        7
    Lullaby  
    OP
       2015-04-11 14:57:21 +08:00
    @anexplore
    我在stackoverflow上看到过这个回答,他说服务器繁忙或网络错误没有正确返回response;
    网上大部分人都说是response header错误;
    不过我自己犯了一个错误,ss的server不是http代理服务,ss的client才是;
    所以换成localhost:port应该就没问题了;
    anyway, thanks so much
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1097 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 19:25 · PVG 03:25 · LAX 11:25 · JFK 14:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.