package com.ots.project.tool.sms; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.params.HttpMethodParams; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URLDecoder; public class InternationalMessage { public final static String INTERNATIONAL_SMS_URL = "http://api2.santo.cc/submit"; public final static String INTERNATIONAL_SMS_COMMAND = "MT_REQUEST"; public static String SendPost(String url, String command, String cpid, String cppwd, String da, String sm, String sa) throws Exception { HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager()); PostMethod method = new PostMethod(); try { URI base = new URI(url, false); method.setURI(new URI(base, "", false)); method.setRequestBody(new NameValuePair[]{ new NameValuePair("command", command), new NameValuePair("cpid", cpid), new NameValuePair("cppwd", cppwd), new NameValuePair("da", da), new NameValuePair("sm", sm), new NameValuePair("sa", sa)}); HttpMethodParams params = new HttpMethodParams(); params.setContentCharset("UTF-8"); method.setParams(params); int result = client.executeMethod(method); if (result == HttpStatus.SC_OK) { InputStream in = method.getResponseBodyAsStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } in.close(); baos.close(); return URLDecoder.decode(baos.toString(), "UTF-8"); } else { throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText()); } } finally { method.releaseConnection(); } } public static String SendGet(String url, String command, String cpid, String cppwd, String da, String sm, String sa) throws Exception { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(); try { URI base = new URI(url, false); method.setURI(new URI(base, "", false)); method.setQueryString(new NameValuePair[]{ new NameValuePair("command", command), new NameValuePair("cpid", cpid), new NameValuePair("cppwd", cppwd), new NameValuePair("da", da), new NameValuePair("sm", sm), new NameValuePair("sa", sa)}); int result = client.executeMethod(method); if (result == HttpStatus.SC_OK) { InputStream in = method.getResponseBodyAsStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } in.close(); baos.close(); return URLDecoder.decode(baos.toString(), "UTF-8"); } else { throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText()); } } finally { method.releaseConnection(); } } public static String query(String url, String cpid, String cppwd) throws Exception { HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true)); GetMethod method = new GetMethod(); try { URI base = new URI(url, false); method.setURI(base); method.setQueryString(new NameValuePair[]{ new NameValuePair("cpid", cpid), new NameValuePair("cppwd", cppwd)}); int result = client.executeMethod(method); if (result == HttpStatus.SC_OK) { InputStream in = method.getResponseBodyAsStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } in.close(); baos.close(); return URLDecoder.decode(baos.toString(), "UTF-8"); } else { throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText()); } } finally { method.releaseConnection(); } } public static byte[] hexStr2ByteArr(String strIn) throws Exception { byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } public static String decodeHexStr(String hexStr) { String realStr = null; if (hexStr != null) { try { byte[] data = hexStr2ByteArr(hexStr); realStr = new String(data, "GBK"); } catch (Exception e) { e.printStackTrace(); } } return realStr; } }