我在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
进行了模糊处理。)
有人可以帮我弄清楚出了什么问题以及如何纠正我的代码吗?