Samples - Java

HTTPS Service

The following samples were created and tested using the JDK version 6. Using the HTTPS Service is just a case of making HTTPS requests from your application. This is quite straight-forward using java.io and java.net.

Sending an SMS using the HTTPS GET method:


    try{
    // Create a URL for the desired page
    URL url = new URL("http://www.textapp.net/webservice/httpservice.aspx
                       ?method=sendsms&returncsvstring=false
                       &externallogin=mylogin&password=mypassword
                       &clientbillingreference=myclientbillingreference
                       &clientmessagereference=myclientmessagereference
                       &originator=mynumber&destinations=%2b447912345678
                       &body=hello%20world&validity=72&charactersetid=2
                       &replymethodid=4&replydata=&statusnotificationurl=");
        
    URLConnection conn = url.openConnection();
    
    // Read all the text returned by the server
    BufferedReader rd = new BufferedReader(new 
    InputStreamReader(conn.getInputStream()));
    String str;
    String result = "";
    while ((str = rd.readLine()) != null) {
    result += str + "\r\n";
    }
    rd.close();
    
    System.console().writer().write(result);
    System.console().flush();
    
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    

Sending an SMS using the HTTPS POST method:


    try{
    // build up the query string
    String data = URLEncoder.encode("method", "UTF-8") + "=" + URLEncoder.encode("sendsms", "UTF-8");
        data += "&" + URLEncoder.encode("returncsvstring", "UTF-8") + "=" + URLEncoder.encode("false", "UTF-8");
        data += "&" + URLEncoder.encode("externallogin", "UTF-8") + "=" + URLEncoder.encode("mylogin", "UTF-8");
        data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode("mypassword", "UTF-8");
        data += "&" + URLEncoder.encode("clientbillingreference", "UTF-8") + "=" + URLEncoder.encode("myclientbillingreference", "UTF-8");
        data += "&" + URLEncoder.encode("clientmessagereference", "UTF-8") + "=" + URLEncoder.encode("myclientmessagereference", "UTF-8");
        data += "&" + URLEncoder.encode("originator", "UTF-8") + "=" + URLEncoder.encode("mynumber", "UTF-8");
        data += "&" + URLEncoder.encode("destinations", "UTF-8") + "=" + URLEncoder.encode("+447912345678", "UTF-8");
        data += "&" + URLEncoder.encode("body", "UTF-8") + "=" + URLEncoder.encode("hello world", "UTF-8");
        data += "&" + URLEncoder.encode("validity", "UTF-8") + "=" + URLEncoder.encode("72", "UTF-8");
        data += "&" + URLEncoder.encode("charactersetid", "UTF-8") + "=" + URLEncoder.encode("2", "UTF-8");
        data += "&" + URLEncoder.encode("replymethodid", "UTF-8") + "=" + URLEncoder.encode("4", "UTF-8");
        data += "&" + URLEncoder.encode("replydata", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
        data += "&" + URLEncoder.encode("statusnotificationurl", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
            
    // Create a URL for the desired page
    URL url = new URL("http://www.textapp.net/webservice/httpservice.aspx");
    
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String str;
    String result = "";
    while ((str = rd.readLine()) != null) {
    result += str + "\r\n";
    }
    wr.close();
    rd.close();
    
    System.console().writer().write(result);
    System.console().flush();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    

If you need to specify proxy details you can do so following the call to url.openConnection();. It will require you to base64 encode the username and password, colon separated :


    System.setProperty("http.proxyHost","proxy_ip");
    System.setProperty("http.proxyPort","proxy_port");
    conn.setRequestProperty( "Proxy-Authorization", “base64 encoded username:password” );