Java Jersey客户端无法连接到远程计算机(502坏网关)

Java Jersey客户端无法连接到远程计算机(502坏网关),java,post,curl,jersey,http-post,Java,Post,Curl,Jersey,Http Post,我无法通过一个小的jersey客户端测试连接到远程机器上运行的服务 我能够使用cURL成功地连接和发布,如下所示: curl-X POST-T test.txt-H“内容类型:应用程序/八位字节流”http://remote-machine:11181/data?start=123 我在远程机器上设置的服务愉快地接受了这一点,并按预期工作,但是当通过jersey客户端实现处理完全相同的URI时,我遇到以下异常: com.sun.jersey.api.client.UniformInterface

我无法通过一个小的jersey客户端测试连接到远程机器上运行的服务

我能够使用cURL成功地连接和发布,如下所示:

curl-X POST-T test.txt-H“内容类型:应用程序/八位字节流”http://remote-machine:11181/data?start=123

我在
远程机器上设置的服务
愉快地接受了这一点,并按预期工作,但是当通过jersey客户端实现处理完全相同的URI时,我遇到以下异常:

com.sun.jersey.api.client.UniformInterfaceException: POST http://remote-machine:11181/data?start=123 returned a response status of 502 Bad Gateway
    at com.sun.jersey.api.client.WebResource.voidHandle(WebResource.java:707)
    at com.sun.jersey.api.client.WebResource.access$400(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:553)
我的泽西岛客户如下:

public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.err.println("Usage: " + TestJerseyClient.class.getName() + " <uri> <input file>");
        System.exit(-1);
    }

    File file = new File(args[1]);
    if (!file.exists()) {
        System.err.println("File not found: " + file.getAbsolutePath());
        System.exit(-2);
    }

    URI uri = URI.create(args[0]);
    Client client = new Client();
    client.setChunkedEncodingSize(16 * 1024);

    FileInputStream stream = new FileInputStream(file);
    try {
        System.out.println("Sending " + file.getPath());
        client.resource(uri).type(MediaType.APPLICATION_OCTET_STREAM).post(stream);
        System.out.println("Done");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Closeables.closeQuietly(stream);
    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
如果(参数长度<2){
System.err.println(“用法:“+TestJerseyClient.class.getName()+”);
系统退出(-1);
}
File File=新文件(args[1]);
如果(!file.exists()){
System.err.println(“未找到文件:+File.getAbsolutePath());
系统出口(-2);
}
URI=URI.create(args[0]);
客户端=新客户端();
客户端.setChunkedEncodingSize(16*1024);
FileInputStream=新的FileInputStream(文件);
试一试{
System.out.println(“发送”+文件.getPath());
client.resource(uri).type(MediaType.APPLICATION\u OCTET\u STREAM).post(STREAM);
系统输出打印项次(“完成”);
}捕获(例外e){
e、 printStackTrace();
}最后{
可关闭。安静关闭(流);
}
}
我的jersey客户端在获得与cURL相同的URI后,会出现502个错误网关错误,如果您能提供帮助,我们将不胜感激


(我想这可能是一个代理问题,但至于让jersey像cURL一样来处理他们,我不知道。)

我通过使用
ApacheHttpClient
而不是标准的jersey
客户端解决了这个问题。不完全确定表面下的差异,但似乎
ApacheHttpClient
比标准Jersey客户端更能适应任何配置和代理,这很可能是创建它的原因之一

我所要改变的是:

Client client = new Client();


当然,也会相应地更新我的依赖项,等等。

这对我来说非常有效,依赖项是com.sun.jersey.contribsjersey-apache-client,您选择的版本应该与现有的jersey客户端版本匹配
Client client = new ApacheHttpClient();