【Spring Boot】发送post请求解析XML
本文最后更新于 1528 天前,其中的信息可能已经有所发展或是发生改变。

[success]这里采用短信发送作为案例,先发送post请求,然后解析xml。[/success]

1.导入Maven。
[sourcecode language=”xml” title=”pom.xml”]
<!– 解析xml –>
<!– https://mvnrepository.com/artifact/dom4j/dom4j –>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
[/sourcecode]

2.编写发送请求和解析类。
[sourcecode language=”java” title=”pom.xml”]
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
public void sendMessage(Verification verification) {
// TODO 短信发送
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("userid", "");
params.add("account", "");
params.add("password", "");
params.add("mobile", verification.getVoContent());
params.add("content", "【笨猪网】您的验证码为["+verification.getVoVerification()+"]");
params.add("action", "send");
params.add("sendTime", "");
params.add("extno", "");
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
HttpMethod method = HttpMethod.POST;
// 以表单的方式提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//将请求头部和参数合成一个请求
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
//执行HTTP请求,将返回的结构使用ResultVO类格式化
ResponseEntity<String> response = client.exchange("请求的地址", method, requestEntity, String.class);
String xml = response.getBody();
try {
Document dom = DocumentHelper.parseText(xml);
Element root=dom.getRootElement();
//写入数据库记录,可以自己写
this.messageInfoMapper.insertMessageInfo(new MessageInfo(null, verification.getVoContent(), root.element("returnstatus").getText(), root.element("message").getText(), root.element("taskID").getText(), DateUtil.getNow()));
} catch (DocumentException e) {
// TODO 处理异常
e.printStackTrace();
}
}
[/sourcecode]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇