1. 程式人生 > >關於簡訊傳送與HTTP請求的那些事

關於簡訊傳送與HTTP請求的那些事

public static boolean sendSmsActivateFriends(String mobile, String content, String type) {
			//簡訊傳送開關
			if(UN_SMS_SWITCH.equals("1")){
				HttpClient client = new HttpClient();
				PostMethod method = new PostMethod(SMSURL);
				client.getParams().setContentCharset("UTF-8");
				method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
				
				NameValuePair[] data = {//提交簡訊引數
						new NameValuePair("account",SMS_ACCOUNT), 
						new NameValuePair("password",SMSPASSWORD),
						new NameValuePair("mobile", mobile), 
						new NameValuePair("content", content),
				};
				method.setRequestBody(data);
				try {
					client.executeMethod(method);
					
					String SubmitResult =method.getResponseBodyAsString();
					
					Document doc = DocumentHelper.parseText(SubmitResult);
					Element root = doc.getRootElement();
					String code = root.elementText("code");	//返回狀態碼列舉
					String msg = root.elementText("msg");	//提交結果的描述
					//String smsid = root.elementText("smsid");	//此訊息的id,在傳送成功的時候返回
					LOGGER.info("互易無線簡訊傳送狀態【{}】,手機號為【{}】,結果【{}】", new Object[]{code,mobile,msg});
					if("2".equals(code)){
						return true;
					}
				} catch (Exception e) {
					LOGGER.error("Exception:【{}】", e);
					LOGGER.error(e.getLocalizedMessage());
				}
				
				return false;
			}else{
				return true;
			}
		}