使用Java向第三方发送短信(第三方 - Site2SMS)

时间:2017-03-08 17:50:13

标签: java web-services authentication sms

我在JAVA中编写了以下代码,使用免费的印度地区网络服务Site2SMS网站向移动设备发送短信。我使用Mashape Api通过密钥进行身份验证。

这是我的代码和错误MSG。



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class SendSMS
{
static String SMSApi = "https://site2sms.p.mashape.com/index.php?msg=Hello&phone=0987654321&pwd=abcdef&uid=XXXXXXXXXX";
static String head = "XXXXXXXXXXXXXXXX";
public static void main(String[] args)
{
    try
    {
        String url = SMSApi;
        String smsApiResponse = sendMySMS(url);
        System.out.println(smsApiResponse);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

private static String sendMySMS(String url)
{
    StringBuilder output = new StringBuilder();
    try
    {
        URL hp = new URL(url);
        System.out.println(url);
        URLConnection hpCon = hp.openConnection();
        hpCon.setRequestProperty("X-Mashape-Key", head);
        BufferedReader in = new BufferedReader(new InputStreamReader(hpCon.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            output.append(inputLine);
        in.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return output.toString();
}
}




当我运行代码时,我得到以下错误响应:



java.io.IOException: Server returned HTTP response code: 403 for URL: https://site2sms.p.mashape.com/index.php?msg=Hello&phone=0987654321&pwd=abcdef&uid=1234567890

	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
	at SendSMS.sendMySMS(SendSMS.java:33)
	at SendSMS.main(SendSMS.java:15)




(存储在Mashape中的head密钥和邮件网址中的uid进行了模糊处理。)

有人可以帮我弄清楚出了什么问题以及如何纠正我的代码吗?

0 个答案:

没有答案