import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class SendSMS { public static String gatewayURL ="https://www.commzgate.net/gateway/SendMsg"; public static void main (String args[]) { String response[] = postNormalMessage("","","","A","test"); System.out.println("You send CG:"+ response[1]); System.out.println("CG returns you:"+ response[0]); } public static String[] postNormalMessage(String ID, String password, String mobile, String type, String message) { String[] response = new String[2]; //Array with 2 parts. Part 1 is what YOU receive from CG. Part 2 is what YOU send try{ HttpURLConnection httpURLConnector = null; DataOutputStream out =null; BufferedReader in =null; URL theURL = new URL(gatewayURL+"?"); httpURLConnector = (HttpURLConnection) theURL.openConnection(); httpURLConnector.setRequestMethod("POST"); httpURLConnector.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); httpURLConnector.setDoInput(true); httpURLConnector.setDoOutput(true); httpURLConnector.setReadTimeout(30000);//30000 httpURLConnector.connect(); out = new DataOutputStream(httpURLConnector.getOutputStream()); StringBuffer sb = new StringBuffer(); sb.append("ID=").append(URLEncoder.encode(ID, "UTF-8")); sb.append("&").append("Password=").append(URLEncoder.encode(password, "UTF-8")); sb.append("&").append("Mobile=").append(URLEncoder.encode(mobile, "UTF-8")); sb.append("&").append("Message=").append(URLEncoder.encode(message, "UTF-8")); sb.append("&").append("Type=").append(URLEncoder.encode(type, "UTF-8")); response[1] = "Posting: "+gatewayURL+"?"+sb.toString(); out.writeBytes(sb.toString()); out.close(); in = new BufferedReader(new InputStreamReader(httpURLConnector.getInputStream())); response[0] = in.readLine(); in.close(); httpURLConnector.disconnect(); } catch(Exception e) { response[0] = "Server Error in posting message ("+e.getMessage()+")"; response[1] = "Unknown -- error in http post! ("+e.getMessage()+")"; }finally { return response; } } }