2013-03-25 116 views
0

我無法連接到通過我敲打的小球衣客戶端測試在遠程計算機上運行的服務。Jersey客戶端無法連接到遠程計算機(502 Bad Gateway)

我能夠連接和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錯誤的網關錯誤。

(我想這或許是一個代理問題,而是讓球衣拿起他們作爲捲曲似乎,我不知道。)

回答

0

我設法通過使用ApacheHttpClient,而不是解決這個的標準澤西島Client。不完全確定表面之下的差異,但似乎ApacheHttpClient似乎在任何配置和代理上比標準的Jersey客戶端更好,這很可能是它創建的原因之一。

所有我不得不改變是:

Client client = new Client(); 

Client client = new ApacheHttpClient(); 

,當然還有我的更新相應依賴性等

+0

這對我的工作很大,依賴是 COM .sun.jersey.contribs jersey-apache-client您選擇的版本應該與您現有的jersey-client版本相匹配上 – 2015-10-23 11:06:24