httpUrlConnection 实现向手机号发送验证码

来源:互联网 发布:分治算法 编辑:程序博客网 时间:2024/04/29 21:26
public static boolean isNetWorkConn(Context context) {
boolean flags = false;
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
return info.isConnected();
}
return flags;
}


public static boolean isMobileNO(String mobiles) {
// Pattern p =
// Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Pattern p = Pattern
.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}


public static String sendCheckCode(Context mcContext, String url,
String telphoneNumber, String check_Code) {
String result = "";
URL url2;
InputStream is;
InputStreamReader isr;
BufferedReader br;
try {
url2 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url2
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");


connection.connect();
OutputStream os = connection.getOutputStream();
// os.write("account=cf_hbws&password=Icebox_1&mobile=15010251210&content=您的验证码是:485613。请不要把验证码泄露给其他人。"
// .getBytes());
os.write(("account=cf_hbws&password=Icebox_1&mobile="
+ telphoneNumber + "&content="
+ mcContext.getString(R.string.your_checkCode) + check_Code + mcContext
.getString(R.string.no_Tell_others)).getBytes());
int code = connection.getResponseCode();


if (code == HttpURLConnection.HTTP_OK) {
is = connection.getInputStream();


isr = new InputStreamReader(is, "UTF-8");
String line = "";
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
result += line;
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
0 0
原创粉丝点击