gpt4 book ai didi

java - 如何使用 xml 正文调用 REST api

转载 作者:行者123 更新时间:2023-11-30 04:57:29 27 4
gpt4 key购买 nike

我有一个用于登录的 REST api ( https://localhost/server/api/login ),它接受 xml 格式的参数,如下所示。

<?xml version="1.0" encoding="UTF-8" ?> 
<Request xmlns="http://www.xxxx.com/center/cbm/1.0.0">
<Users>
<User>
<Type>userType</Type>
<Name>admin</Name>
<Password>password</Password>
<Captcha>On</Captcha>
</User>
</Users>
</Request>

如何使用 Java 调用它并接收响应。

最佳答案

您可以使用 Apache HTTP Client 4.x 执行 HTTP POST 请求:

String xmlString = "... your data ..."

HttpPost httpRequest = new HttpPost("https://localhost/server/api/login");
httpRequest.setEntity(new StringEntity(xmlString));

HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httpRequest, new BasicHttpContext());

if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK && httpResponse.getEntity() != null) {
//handle response ok
} else {
//handle error
}

关于java - 如何使用 xml 正文调用 REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8075452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com