无法从 spring resttemplate 访问短信网关
Can't access the sms gateway from spring resttemplate
我有短信网关,我可以给 url 发送短信,如下所示。
"https://url/destination={mobileNum}&q=mypw\n&message={msg}\n"
我已经在 resttemplate 中使用它并尝试发送短信但是它没有用。
public String send(String mobileNumber, String message) {
String uri = "https://url/destination={mobileNum}&q=mypw\n&message={msg}\n";
RestTemplate restTemplate = new RestTemplate();
URI result = restTemplate.postForLocation(uri, mobileNumber, message);
return result.toString();
//return null;
}
代码的其他部分工作正常。它不会给出任何错误。但是短信发送部分不起作用。我该如何解决这个问题?
我猜你在 URL 中使用的 new-line
字符有问题。尝试逃跑 \
使用 queryParams 时 URL 的另一个问题。查询参数应该在 ?
分隔符之后。
所以最终 URL 可以看起来像:
String uri = "https://url?destination={mobileNum}&q=mypw\n&message={msg}\n";
试试这个。
public String send(String mobileNumber, String message) {
String uri = "https://url/destination=" mobileNumber + "&q=mypw&message="+message;
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
return result;
}
根据 RestTemplate 文档,方法 postForLocation 用于通过将给定的 object 发布到 URI 模板和 returns 位置的值 header 来创建新资源. header 通常表示新资源的存储位置。 RestTemplate docs
服务器可能不允许查询新资源的位置,因此不会发送位置 header 作为响应。
读取 URL 时出错。 "mypw&message" 被用作密码,而不仅仅是 "mypw"。我在使用 jboss 调试模式调试代码时发现了它。
这是最终代码
@Service
public class SmsServiceImpl implements SmsService{
@Override
public String send(String mobileNumber, String message) {
String uri = "https://urlTodestination="+mobileNumber+"&q=pw&message="+message;
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
return result;
}
}
我有短信网关,我可以给 url 发送短信,如下所示。
"https://url/destination={mobileNum}&q=mypw\n&message={msg}\n"
我已经在 resttemplate 中使用它并尝试发送短信但是它没有用。
public String send(String mobileNumber, String message) {
String uri = "https://url/destination={mobileNum}&q=mypw\n&message={msg}\n";
RestTemplate restTemplate = new RestTemplate();
URI result = restTemplate.postForLocation(uri, mobileNumber, message);
return result.toString();
//return null;
}
代码的其他部分工作正常。它不会给出任何错误。但是短信发送部分不起作用。我该如何解决这个问题?
我猜你在 URL 中使用的 new-line
字符有问题。尝试逃跑 \
使用 queryParams 时 URL 的另一个问题。查询参数应该在 ?
分隔符之后。
所以最终 URL 可以看起来像:
String uri = "https://url?destination={mobileNum}&q=mypw\n&message={msg}\n";
试试这个。
public String send(String mobileNumber, String message) {
String uri = "https://url/destination=" mobileNumber + "&q=mypw&message="+message;
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
return result;
}
根据 RestTemplate 文档,方法 postForLocation 用于通过将给定的 object 发布到 URI 模板和 returns 位置的值 header 来创建新资源. header 通常表示新资源的存储位置。 RestTemplate docs
服务器可能不允许查询新资源的位置,因此不会发送位置 header 作为响应。
读取 URL 时出错。 "mypw&message" 被用作密码,而不仅仅是 "mypw"。我在使用 jboss 调试模式调试代码时发现了它。
这是最终代码
@Service
public class SmsServiceImpl implements SmsService{
@Override
public String send(String mobileNumber, String message) {
String uri = "https://urlTodestination="+mobileNumber+"&q=pw&message="+message;
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
return result;
}
}