使用HttpURLConnection发送短信

来源:互联网 发布:数据结构排序算法心得 编辑:程序博客网 时间:2024/05/16 04:55

public int sendMobile(String receiveMobile, String content) {

/**设置短信参数**/
String msg_ip = "";
String msg_user_id = "";
String msg_company_id = "";
String msg_pwd = "";
String msg_key = "";



msg_ip = Config.MSG_IP;
msg_user_id = Config.MSG_USER_ID;
msg_company_id = Config.MSG_COMPANY_ID;
        msg_pwd = Config.MSG_PWD;
        msg_key = Config.MSG_KEY;
   
/**发动短信动作**/
try {
content =  URLEncoder.encode(content+msg_key, "utf-8"); 
System.out.println(content);
String url  = msg_ip+"corporateid="+msg_company_id+"&userid="+msg_user_id+"&password="+msg_pwd+"&mobile="+receiveMobile+"&content="+content+"&schtime=&subcode=";
URL urls = new URL(url); 
HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
//connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//
String lines; 
   while ((lines = reader.readLine()) != null){ 
       //lines = new String(lines.getBytes(), "utf-8"); 
       System.out.println(lines); 
   } 
   reader.close(); 
   connection.disconnect(); 
} catch (Exception e) {
   e.printStackTrace();
       try {
   throw e;
   
   } catch (Exception e1) {
e1.printStackTrace();
}
   return 0;
}
return 1;
}
0 0