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>
添加回答
舉報