关于 http 代理请求返回 Unexpected end of file from server? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Lullaby
V2EX    Java

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

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

    测试代码如下
    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     Solana     5182 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 07:43 PVG 15:43 LAX 23:43 JFK 02:43
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86