Java 捕获服务器响应代码

Java 捕获服务器响应代码,java,api,sms,response,Java,Api,Sms,Response,我正在尝试使用上提供的API从Java web应用程序发送SMS 我无法收到教程中提到的响应代码,可以找到。我正在为我的项目使用Java版本的代码。谁能提供解决方案 响应代码如下: 1 : SMS sent -1 : Server Error -2 : Invalid Username -3 : Invalid message text -4 : Login Failed -5 : IP Blocked import java.net.*; import java.io.*;

我正在尝试使用上提供的API从Java web应用程序发送SMS

我无法收到教程中提到的响应代码,可以找到。我正在为我的项目使用Java版本的代码。谁能提供解决方案

响应代码如下:

1 : SMS sent -1 : Server Error -2 : Invalid Username -3 : Invalid message text -4 : Login Failed -5 : IP Blocked
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);
  }
}

我无法接收响应代码。。。这是什么意思?你有例外吗?你发现你的
println
没有打印任何东西吗?另外,我很佩服你有耐心使用一个API,这个API是由一个人编写的,他认为可以通过在他们的网页前浮动45秒的消息来强迫人们“喜欢”他们的API。太棒了。@DuncanJones。。它不打印任何东西。它可以打印空字符串,对我来说,它打印“-2”。使用
System.out.println(“返回代码:+br.readLine())@ajozwik您是尝试了相同的代码还是做了一些更改?
InputStream response = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(response));
String responseString = br.readLine();
Integer responseInt = Integer.valueOf(responseString).intValue();
switch(responseInt)
{
  case 1 : //SMS sent
  break;
  case -1 : // Server error
  break;
.
.  // put check conditions similarly
.
}