首页 > 解决方案 > 使用 Postman 调用 WS 已完成,但不是从 Java 代码

问题描述

我使用 Postman 发出了一个 Post 请求并得到了响应,但是什么时候在 java 中使用 OkHttpClient(与 HttpsURLConnection 相同的问题)进行相同的请求时,我得到了一个连接被拒绝的异常。

下面是我的代码(带有假数据):

OkHttpClient client = new OkHttpClient();
String req = "<?xml version=\"1.0\"?>\r\n" + 
 "<ApplicantTestRequest\r\n" + 
 "PositionID=\"48939014-b24f-4d74-8a44-9913cd9f8936\"\r\n" + 
 "ThirdPartyCandidateID=\"4152ab4r\"\r\n" + 
 "FirstName=\"Danny\"\r\n" + 
 "LastName=\"Givaty\"\r\n" + 
 "UserName=\"dannyg\"\r\n" + 
 "Password=\"2sEr#d!w@\"\r\n" + 
 "email=\"dannyg@careerharmony.com\"\r\n" + 
 "Telephone=\"5558586858\"\r\n" + 
 "Source=\"LinkeIn\"\r\n" + 
 "SkipToFirstRecruiterComponent = \"1\"\r\n" + 
 "Gender = \"1\"\r\n" + 
 "/>";

RequestBody reqbody = RequestBody.create(null, req);  

Request request = new Request.Builder()
 .url("https://staging.direct-assessment.net/RomaTestUI/forms/xmlregistrationandtestentry.aspx?XMLReadType=1")
 .method("POST",reqbody)
 .addHeader("Content-Type", "application/x-www-form-urlencoded")
 .addHeader("cache-control", "no-cache")
 .build();

Response response = client.newCall(request).execute();
int responseCode = response.code();
System.out.println("Response Code : " + responseCode);

我得到的结果是:

Exception in thread "main" java.net.ConnectException: Failed to connect to staging.direct-assessment.net/185.52.110.193:443
...........
Caused by: java.net.ConnectException: Connection refused: connect  

有什么建议吗?

标签: javaposthttpspostman

解决方案


我可以看到以下原因,我还包括一个可能的解决方案 -

该问题可能与 HTTP_TRANSPORT_VERSION 有关,您能否检查通过 Postman 发送的版本以及从 Java 程序发送的版本。如果版本不同,则在您的 java 调用中设置 HTTP_TRANSPORT_VERSION(类似于 Postman 请求)。


推荐阅读