簡體   English   中英

無法從Spring RestTemplate訪問SMS網關

[英]Can't access the sms gateway from spring resttemplate

我有短信網關,可以提供網址並發送短信,如下所示。

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字符存在問題。 試圖逃跑\\

URL中使用queryParams的另一個問題。 查詢參數應該在之后? 分米。

因此,最終URL可能類似於:

String uri = "https://url?destination={mobileNum}&q=mypw\\n&message={msg}\\n";

URL的組成部分

嘗試這個。

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用於通過將給定對象發布到URI模板來創建新資源,並返回Location標頭的值。 此標頭通常指示新資源的存儲位置。 RestTemplate文檔

服務器可能不允許查詢新資源的位置,因此將不會發送位置標頭作為響應。

讀取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;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM