way2sms API在Java中无法工作

3

我在我的网络应用程序中使用way2sms api。 以前它运作良好,但现在无法发送短信。 有人能告诉我突然出了什么问题吗? api已过时吗? 或者way2sms网站或网关有问题吗?


1
你没有收到任何错误信息或异常吗? - Joachim Sauer
我在我的代码中使用了异常处理,但它显示无法发送。你想让我发布我的代码吗? - Kanchan Ruia
1
如果你只收到了“无法发送”的信息,那么你做错了。异常类型是什么?堆栈跟踪是什么?确切的错误消息是什么?你是否有堆栈跟踪?或者“使用异常处理”是否意味着你写了 catch (Exception e) { System.out.println("could not be send"); }?如果是这样,请重新学习什么是异常处理 - Joachim Sauer
不完整的问题。除非您发布与之相关的一些信息,否则我们怎么能知道突然出了什么问题。请正确地提出您的问题。 - Suhail Gupta
1个回答

1

这是我写的一些代码,希望你会觉得有用。

import java.net.*;
import java.io.*;

public class SmsSender
{
  //Replace your way2sms username and password below
  static final String _userName = "your way2sms username";
  static final String _password = "your way2sms password";
  static final String _url = "http://ubaid.tk/sms/sms.aspx";
  static final String charset = "UTF-8";

  //to build the query string that will send a message
  private static String buildRequestString(String targetPhoneNo, String message) throws UnsupportedEncodingException
  {
    String [] params = new String [5];
    params[0] = _userName;
    params[1] = _password;
    params[2] = message;
    params[3] = targetPhoneNo;
    params[4] = "way2sms";

    String query = String.format("uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s",
        URLEncoder.encode(params[0],charset),
        URLEncoder.encode(params[1],charset),
        URLEncoder.encode(params[2],charset),
        URLEncoder.encode(params[3],charset),
        URLEncoder.encode(params[4],charset)
        );
    return query;
  }

  public static void sendMessage(String reciever, String message) throws Exception
  {
    //To establish the connection and perform the post request
    URLConnection connection = new URL(_url + "?" + buildRequestString(reciever,message)).openConnection();
    connection.setRequestProperty("Accept-Charset", charset);

    //This automatically fires the request and we can use it to determine the response status
    InputStream response = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(response));
    System.out.println(br.readLine());
  }

  public static void main(String [] args)
    throws Exception
  {
    String testPhoneNo = "9876543210";
    String testMessage = "Sending Messages From java is not too hard";

    sendMessage(testPhoneNo,testMessage);
  }
}

是的,way2sms已经停止工作,你可以尝试其他提供商。提供API的人已经转向付费模式,我没有时间去编写替代API。其他一些提供商确实可以使用,你需要花些时间找出哪些可用,哪些不可用。 - nikhil
尝试这个:http://ayushpateria.com/way2sms.php,我不确定该网站的所有者是否存储凭据。 - Yauraw Gadav
@nOiAd 请检查您发布的链接。它似乎会重定向回way2sms网站。 - nikhil
是的,该链接只是重定向请求。 - Yauraw Gadav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接