2 回答
TA贡献1946条经验 获得超3个赞
恕我直言,api 调用是最佳选择。RestTemplate请使用如下所示使用已发布的 API 进行实施。
public void sendOTP() {
RestTemplate restTemplate = new RestTemplate();
String message = "Your PIN for account verification is 123456";
String user = "******405e4c****19d0******";
String password = "******";
String smsurl = "https://api.twilio.com/2010-04-01/Accounts/"+user+"/Messages.json";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("From", "+1334384****");
map.add("To", "+999999999");
map.add("Body", message);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, headers);
try {
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));
Object response = restTemplate.postForObject(smsurl, httpEntity, Object.class);
LOG.info("Sms Response: {}", gson.toJson(response));
} catch(Exception e) {
LOG.error(e.getMessage());
}
}
TA贡献1784条经验 获得超8个赞
您认为客户端没有指定内容类型。请补充content-type: application/xml。
如果你有 spring boot,你可以通过添加以下依赖项来修复它:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
添加回答
举报