我正在尝试使用java程序将短信从我的手机号码发送到另一个号码, 我不允许使用任何第三方网关。我的代码如下:
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.net.ssl.HttpsURLConnection;
class SendSMS
{
public static void main(String args[])
{
boolean debug=true;
String phone ="10 digit number";
String message ="hi";
String ppgHost ="http://localhost:8800/";
String username ="";
String password ="";
try
{
phone=URLEncoder.encode("8366xxx", "UTF-8");
if(debug)
System.out.println("phone------>"+phone);
message=URLEncoder.encode("SendMsg via Now.SMS", "UTF-8");
if(debug)
System.out.println("message---->"+message);
}
catch (UnsupportedEncodingException e)
{
System.out.println("Encoding not supported");
}
String url_str=ppgHost+"?PhoneNumber="+phone+"&Text="+message;
//String url_str=ppgHost+"?user="+username+"&password="+password+"&PhoneNumber="+phone+"&Text="+message;
if(debug)
System.out.println("url string->"+url_str);
try
{
URL url2=new URL(url_str);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
if(debug)
System.out.println("Opened Con->"+connection);
String res=connection.getResponseMessage();
if(debug)
System.out.println("Get Resp ->"+res);
int code = connection.getResponseCode () ;
if ( code == HttpURLConnection.HTTP_OK )
{
connection.disconnect() ;
}
}
catch(IOException e)
{
System.out.println("unable to create new url"+e.getMessage());
}
}
}
请指导我们。我使用数据线将手机连接到我的系统并尝试发送,但我收到的错误如下:
C:\SMS>java SendSMS
phone------>8366xxx
message---->SendMsg+via+Now.SMS
url string->http://localhost:8800/?PhoneNumber=8366xxx&Text=SendMsg+via+Now.SMS
Opened Con->sun.net.www.protocol.http.HttpURLConnection:http://localhost:8800/?P
honeNumber=8366xxx&Text=SendMsg+via+Now.SMS
unable to create new urlConnection refused: connect
请帮助家伙
我在这里做错了什么?我是新来的。几乎所有的例子都是使用第三方库和网关。帮帮我们!