/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package reportbuilder; import java.io.*; import java.net.*; import java.util.*; import java.nio.charset.Charset; import java.nio.CharBuffer; import java.nio.ByteBuffer; /** * * @author Administrator */ public class SendSMS { final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for ( int i = 0; i < bytes.length; i++ ) { int v = bytes[i] & 0xFF; hexChars[i * 2] = hexArray[v >>> 4]; hexChars[i * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); } public static String toHexString(String s, String charset) { try { return bytesToHex(s.getBytes(charset)); } catch(UnsupportedEncodingException e) { System.out.println("Unsupported character set" + e); return null; } } public static String convertCharset(String text, String from, String to) { Charset fromCharset = Charset.forName(from); Charset toCharset = Charset.forName(to); ByteBuffer inputBuffer = fromCharset.encode(CharBuffer.wrap(text)); CharBuffer data = fromCharset.decode(inputBuffer); ByteBuffer outputBuffer = toCharset.encode(data); byte[] outputData = outputBuffer.array(); return new String(outputData, toCharset); } public static String send(String urls,String RefNo,String User,String Password,String Sender,String Msn,String text){ //BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream("utf8.txt"), "UTF8")); //String text = args[5];//fin.readLine(); try{ String msg = convertCharset(text, "UTF-8", "UTF-16BE"); URL url = new URL(urls); Map params = new LinkedHashMap(); params.put("User", User); params.put("Password", Password); params.put("Sender", Sender); params.put("Msn", Msn); params.put("RefNo", RefNo); params.put("MsgType", "H"); params.put("Encoding", "25"); params.put("Msg", toHexString(msg, "UTF-16BE")); //params.put("Msg", args[5]); StringBuilder postData = new StringBuilder(); for (Map.Entry param : params.entrySet()) { if (postData.length() != 0) postData.append('&'); postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); postData.append('='); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); conn.setDoOutput(true); conn.getOutputStream().write(postDataBytes); Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); StringBuilder sb = new StringBuilder(); for (int c; (c = in.read()) >= 0;) sb.append((char)c); String response = sb.toString(); System.out.println(response); return response; }catch(Exception e){ e.printStackTrace(); return "false:"+e.getMessage(); } } public static void main(String[] args) throws Exception { SendSMS.send(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); } }