Sending a text through txtlocal.com (or how to do a post to a page)

The java examples they provide require the use of an apache library that has superseded. So I wrote my own using simple java libraries. No extras needed.

Obviously you’ll have to get yourself a txt local free text account before you go ahead….

http://www.txtlocal.co.uk/?tlrx=89707

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package phj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 *
 * @author paulc
 */
public class SMSSender {



    public String sendSms(String sToPhoneNo,String sMessage) {

    try {
    // Construct data
    String data = URLEncoder.encode("uname", "UTF-8") + "=" + URLEncoder.encode("txtlocalusername****", "UTF-8");
    data += "&" + URLEncoder.encode("pword", "UTF-8") + "=" + URLEncoder.encode("txtlocalpassword****", "UTF-8");
    data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode(sMessage, "UTF-8");
    data += "&" + URLEncoder.encode("from", "UTF-8") + "=" + URLEncoder.encode("from email address", "UTF-8");
    data += "&" + URLEncoder.encode("selectednums", "UTF-8") + "=" + URLEncoder.encode(sToPhoneNo, "UTF-8");
    data += "&" + URLEncoder.encode("info", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
    // Send data
    URL url = new URL("https://www.txtlocal.com/sendsmspost.php");
    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 line;
    String sResult="";
    while ((line = rd.readLine()) != null) {
        // Process line...
        sResult=sResult+line+" ";
    }
    wr.close();
    rd.close();
    return sResult;
} catch (Exception e) {
    System.out.println("Error SMS "+e);
    return "Error "+e;
}

    }
}

Obviously mine is more complex now (hence not final/static), just replace username, password and from email.

 

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on September 9, 2011, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: