Samples - Java

HTTP(S) Service

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

Sending an SMS using the HTTP(S) GET method:

try{ // Create a URL for the desired page URL url = new URL("https://ws.textanywhere.net/HTTPRX/SendSMSEx.aspx?Client_ID=my_id&Client_Pass=my_pass&Client_Ref=my_ref&Billing_Ref=billing_ref&Connection=2&Originator=%2b447912345678&OType=0&DestinationEx=%2b447923456789&Body=hello%20world&SMS_Type=0&Reply_Type=0"); 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 HTTP(S) POST method:

try{ // build up the query string String data = URLEncoder.encode("Client_ID", "UTF-8") + "=" + URLEncoder.encode("my_id", "UTF-8"); data += "&" + URLEncoder.encode("Client_Pass", "UTF-8") + "=" + URLEncoder.encode("my_pass", "UTF-8"); data += "&" + URLEncoder.encode("Client_Ref", "UTF-8") + "=" + URLEncoder.encode("my_ref", "UTF-8"); data += "&" + URLEncoder.encode("Billing_Ref", "UTF-8") + "=" + URLEncoder.encode("billing_ref", "UTF-8"); data += "&" + URLEncoder.encode("Connection", "UTF-8") + "=" + URLEncoder.encode("2", "UTF-8"); data += "&" + URLEncoder.encode("Originator", "UTF-8") + "=" + URLEncoder.encode("+447912345678", "UTF-8"); data += "&" + URLEncoder.encode("OType", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8"); data += "&" + URLEncoder.encode("DestinationEx", "UTF-8") + "=" + URLEncoder.encode("+447923456789", "UTF-8"); data += "&" + URLEncoder.encode("Body", "UTF-8") + "=" + URLEncoder.encode("hello world", "UTF-8"); data += "&" + URLEncoder.encode("SMS_Type", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8"); data += "&" + URLEncoder.encode("Reply_Type", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8"); // Create a URL for the desired page URL url = new URL("https://ws.textanywhere.net/HTTPRX/SendSMSEx.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” );

Footer Bottom