Jersey客户端无法连接到远程计算机(502 Bad Gateway)

时间:2013-03-25 14:20:31

标签: java post curl jersey http-post

我无法通过小型球衣客户端测试连接到我在远程计算机上运行的服务。

我可以像这样使用cURL成功连接和POST:

curl -X POST -T test.txt -H "Content-Type: application/octet-stream" http://remote-machine:11181/data?start=123

我在remote-machine上设置的服务很乐意接受这个并按预期工作,但是当通过我的泽西客户端实现处理完全相同的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);
    }
}

我们非常感谢任何帮助,为什么我的泽西客户端在获得与cURL相同的URI时,会收到这些502 Bad Gateway错误。

(我想它也许可能是一个代理问题,但是就像cURL似乎让球衣接受它们一样,我不知道。)

1 个答案:

答案 0 :(得分:0)

我设法通过使用ApacheHttpClient而不是标准的泽西Client来解决这个问题。不完全确定表面下的差异,但似乎ApacheHttpClient似乎比标准的Jersey客户端更好地接受任何配置和代理,这很可能是它创建的原因之一。

我必须改变的是:

Client client = new Client();

Client client = new ApacheHttpClient();

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