/** * 检测发送短信验证码所需字段是否完整,且不能为空,符合基本规则 * * @return */ private boolean checkRequestParameter() { if (StringUtils.isNotEmpty(userid) && StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)) { return true; } return false; }
/** * 使用httpclient发起post请求 * * @param mobile 目的号码,必须为手机号 * @param content 短信内容信息 * @return xml解析后的的hashMap */ public Map<String, String> phoneText(String mobile, String content) { // 验证参数是否正确, 错误则返回为空值 if (!checkRequestParameter() || !StringUtils.isNotEmpty(mobile) || !StringUtils.isNotEmpty(content)) { return null; } // 封装post请求参数 List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("action", send_action)); postParameters.add(new BasicNameValuePair("userid", userid)); postParameters.add(new BasicNameValuePair("account", account)); postParameters.add(new BasicNameValuePair("password", password)); postParameters.add(new BasicNameValuePair("mobile", mobile)); postParameters.add(new BasicNameValuePair("content", content)); postParameters.add(new BasicNameValuePair("sendTime", "")); postParameters.add(new BasicNameValuePair("checkcontent", checkcontent)); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse closeableHttpResponse = null; HttpPost httpPost = new HttpPost(send_url); UrlEncodedFormEntity urlEncodedFormEntity; Map<String, String> responseMap = null; // 发送post请求 try { // 根据文档提示,使用utf-8格式编码 urlEncodedFormEntity = new UrlEncodedFormEntity(postParameters, "utf-8"); httpPost.setEntity(urlEncodedFormEntity); closeableHttpResponse = httpClient.execute(httpPost); if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = closeableHttpResponse.getEntity(); if (httpEntity != null) { // 将响应返回的xml解析为HashMap responseMap = parseXML(httpEntity.getContent()); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { closeableHttpResponse.close(); } catch (IOException e) { e.printStackTrace(); } try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return responseMap; }