java - 使用 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
有什么建议吗?
解决方案
我可以看到以下原因,我还包括一个可能的解决方案 -
该问题可能与 HTTP_TRANSPORT_VERSION 有关,您能否检查通过 Postman 发送的版本以及从 Java 程序发送的版本。如果版本不同,则在您的 java 调用中设置 HTTP_TRANSPORT_VERSION(类似于 Postman 请求)。
推荐阅读
- python - 在 python 中,如何创建一个名称未指定的新变量,直到运行时(编辑:如何将 setattr() 与列表组合一起使用)?
- python - 如何在另一个 Python 类构造函数中实例化 Python 类?
- hazelcast - Hazelcast 3.8.2 中的内存泄漏
- git - 有什么方法可以在同一个文件夹中拥有版本化和非版本化的 .gitignore?
- php - PHP 从单个列中获取自定义标签并将它们分开
- python - 变分自动编码器只是输出训练集的平均值
- angular - TypeError:无法读取 null 的属性“canDeactivate”
- javascript - 用于从 url 中提取不带扩展名的文件名的正则表达式
- javascript - 将 JavaScript 函数作为函数指针传递给 C++ dll
- bash - 递归替换多个文件中的字符串