公共类SmsClientSend { / * * @param url *:必填 - 发送连接地址URL - http://dc.28inter.com/sms.aspx * @param userid *:必填 - 用户ID,为数字 * @param account *:必填 - 用户帐号 * @param密码 *:必填 - 用户密码 * @param mobile *:必填 - 发送的手机号码,多个可以用逗逗隔比如> 130xxxxxxxx ,131xxxxxxxx * @param content *:必填 - 实际发送内容, * @param action *:选填 - 访问的事件,默认为send * @param sendType *:选填 - 发送方式,默认为POST * @param codingType *:选填 - 发送内容编码方式,默认为UTF-8 * @param backEncodType *:选填 - 返回内容编码方式,默认为UTF-8 * @return返回发送之后收到的信息 * / private static String sendSms(String url,String userid,String account, String password,String mobile,String content,String action, String sendType,String codingType,String backEncodType){ try { if(codingType == null || codingType.equals("")){ codingType ="UTF-8" } 如果(backEncodType == NULL || backEncodType.equals( "")){ backEncodType = "UTF-8"} StringBuffer send = new StringBuffer(); if(action!= null &&!action.equals( "")){ send.append("action =")。append(action); } else { send.append("action = send"); } send.append( "&用户id =")附加(用户ID)。 send.append("&account =")。append( URLEncoder.encode(account,codingType)); send.append("&password =")。append( URLEncoder.encode(password,codingType)); send.append("&mobile ="),append ; send.append("&content ="),append ; URLEncoder.encode(content,codingType)); if(sendType!= null &&(sendType.toLowerCase())。equals("get")){ return SmsClientAccessTool.getInstance()。doAccessHTTPGet( url +“?”+ send.toString(),backEncodType); } else { return SmsClientAccessTool.getInstance()。doAccessHTTPPost(url, send.toString(),backEncodType); } }(例外){ e.printStackTrace(); return“未发送,编码异常” } } } 公共类SmsClientAccessTool { private static SmsClientAccessTool smsClientToolInstance; / ** *采用单列方式来访问操作 * * @return * / public static synchronized SmsClientAccessTool getInstance(){ if(smsClientToolInstance == null){ smsClientToolInstance = new SmsClientAccessTool(); } return smsClientToolInstance; } / ** * * POST方法 * * * @param sendUrl *:访问URL * @param paramStr *:参数串 * @param backEncodType *:返回的编码 * @return * / public String doAccessHTTPPost(String sendUrl,String sendParam, String backEncodType){ StringBuffer receive = new StringBuffer(); BufferedWriter wr = null; try { if(backEncodType == null || backEncodType.equals("")){ backEncodType ="UTF-8" } URL url =新网址(sendUrl); HttpURLConnection URLConn =(HttpURLConnection)url .openConnection(); URLConn.setDoOutput(真); URLConn.setDoInput(真); ((HttpURLConnection)URLConn)。setRequestMethod("POST"); URLConn.setUseCaches(假); URLConn.setAllowUserInteraction(真); HttpURLConnection.setFollowRedirects(真); URLConn.setInstanceFollowRedirects(真); URLConn.setRequestProperty("Content-Type", “application / x-www-form-urlencoded; charset = UTF-8); URLConn.setRequestProperty("Content-Length",String .valueOf(sendParam.getBytes()。length)); DataOutputStream dos = new DataOutputStream(URLConn .getOutputStream()); dos.writeBytes(sendParam); BufferedReader rd = new BufferedReader(new InputStreamReader( URLConn.getInputStream(),backEncodType)); 字符串行; while((line = rd.readLine())!= null){ receive.append(line).append("\ r \ n"); } rd.close(); } catch(java.io.IOException e){ receive.append("访问产生了异常 - >").append(e.getMessage()); e.printStackTrace(); } finally { if(wr!= null){ try { wr.close(); } catch(IOException ex){ ex.printStackTrace(); } WR = NULL; } } return receive.toString(); } public String doAccessHTTPGet(String sendUrl,String backEncodType){ StringBuffer receive = new StringBuffer(); BufferedReader in = null; try { if(backEncodType == null || backEncodType.equals("")){ backEncodType ="UTF-8" } URL url =新网址(sendUrl); HttpURLConnection URLConn =(HttpURLConnection)url .openConnection(); URLConn.setDoInput(真); URLConn.setDoOutput(真); URLConn.connect(); URLConn.getOutputStream()平齐(); in = new BufferedReader(new InputStreamReader(URLConn .getInputStream(),backEncodType)); 字符串行; while((line = in.readLine())!= null){ receive.append(line).append("\ r \ n"); } } catch(IOException e){ receive.append("访问产生了异常 - >")。append(e.getMessage()); e.printStackTrace(); } finally { if(in!= null){ try { in.close(); } catch(java.io.IOException ex){ ex.printStackTrace(); } in = null; } } return receive.toString(); } } |