V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Lullaby
V2EX  ›  Java

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

  •  
  •   Lullaby · Apr 10, 2015 · 30369 views
    This topic created in 4046 days ago, the information mentioned may be changed or developed.

    测试代码如下
    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 replies    2015-04-11 14:57:21 +08:00
    Lullaby
        2
    Lullaby  
    OP
       Apr 10, 2015
    @anexplore url是我改过的,换其他的也都一样;有可能是代理服务器的问题
    zhicheng
        3
    zhicheng  
       Apr 10, 2015 via Android
    一般情况是,服务器设置了 content-length 但在客户端接收到 content-length 的 body 之前服务器或代理就把连接 close 掉了。你要检查一下,服务器和代理是不是有请求大小限制或请求时间限制。
    Lullaby
        4
    Lullaby  
    OP
       Apr 10, 2015
    代理服务器设在日本,开了ss代理,开放了8888端口接收代理请求,现在用httpclient设置代理过去,老是报The target server failed to respond
    x.x.x.x failed to respond
    Lullaby
        5
    Lullaby  
    OP
       Apr 10, 2015
    @zhicheng 不知道是不是你说的那样 回去研究下
    eycfsjd
        6
    eycfsjd  
       Apr 10, 2015
    @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
       Apr 11, 2015
    @anexplore
    我在stackoverflow上看到过这个回答,他说服务器繁忙或网络错误没有正确返回response;
    网上大部分人都说是response header错误;
    不过我自己犯了一个错误,ss的server不是http代理服务,ss的client才是;
    所以换成localhost:port应该就没问题了;
    anyway, thanks so much
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5048 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 03:57 · PVG 11:57 · LAX 20:57 · JFK 23:57
    ♥ Do have faith in what you're doing.