我做了一些研究,有很多地方说如何使用httpsurlconnection在java中发布url参数,还有许多源代码指定如何将soapxml有效负载作为post请求的主体发布。但在发帖要求中,没有地方说明如何同时做到这两个方面。我正在尝试编写一个请求来发布参数和一个xml主体,但不知道如何正确格式化请求。
public String apiCall(String xmlData) {
String dataBack = "";
String base64login = "";
String login = "user:password";
base64login = new String(Base64.encodeBase64(login.getBytes()));
String tServer = "https://yourwebsite/pageToPostDataTo";
String urlParams = "firstAction=DoThis&secondAction=doThat";
byte[] postData = urlParams.getBytes(StandardCharsets.UTF_8);
URL url = new URL(tServer);
HttpsURLConnection con = (HttpsUrlConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/xml");
con.setRequestProperty("Authentication", "Basic " + base64login);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.getOutputStream().write(postData);
con.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
try (OutputStream os = con.getOutputStream()) {
byte[] input = xmlData.getBytes("utf-8");
os.write(input);
}
InputStream inStream = con.getInputStream();
byte[] res = new byte[2048];
int i = 0;
StringBuilder response = new StringBuilder();
while ((i = inStream.read(res)) != -1) {
res.append(new String(res, 0, 1));
}
inStream.close();
dataBack += response;
}
但是,这会导致soap请求以“prolog中不允许的内容”响应
暂无答案!
目前还没有任何答案,快来回答吧!