发送http请求工具类

来源:互联网 发布:seo学习 编辑:程序博客网 时间:2024/05/16 06:51
注意:此方法有针对性,需要修改成自己的方法。/** * 2017年10月17日下午3:58:08 */package com.jjmc.dcl.util;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.security.MessageDigest;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Map;import org.apache.log4j.Logger;import com.jjmc.dcl.constant.Constant;/** * * @author huangtao * 2017年10月19日下午10:37:59 * dclTask * @parameter * TODO * */public class MessageUtil {//日志private static final StringDEBUG_PREFIX = "[MessageUtil]";private static final StringINFO_PREFIX = "<MessageUtil>";private static final StringERROR_PREFIX = "MessageUtil->";private static Logger logger= Logger.getLogger(MessageUtil.class);/*** MD5* @param plainText* @return*/public static String md5(String plainText) {String re_md5 = new String();try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(plainText.getBytes("UTF-8"));byte b[] = md.digest();int i;StringBuffer buf = new StringBuffer("");for (int offset = 0; offset < b.length; offset++) {i = b[offset];if (i < 0)i += 256;if (i < 16)buf.append("0");buf.append(Integer.toHexString(i));}re_md5 = buf.toString();} catch (Exception e) {e.printStackTrace();}return re_md5;}/*** get方法发送短信* 只发送普通短信* List<String> mobilePhones电话号码* String content短信内容* * * */public static Object getSendSmsMessage(List<String> mobilePhones,String content) throws Exception{logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",getSendMessage begin");logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",getSendMessage begin");try{//从配置文件中读取发送短信的参数String username = ResourceUtil.getConf("sms.message.username");String password = ResourceUtil.getConf("sms.message.password");String messageUrl = ResourceUtil.getConf("sms.message.get.url");StringBuffer sb = new StringBuffer(messageUrl);sb.append("username="+username);String passwordN = md5(username+md5(password));sb.append("&password="+passwordN);sb.append("&mobile=");for(String mp:mobilePhones){sb.append(mp+",");}//去掉最后一个逗号sb.deleteCharAt(sb.length() - 1);sb.append("&content="+URLEncoder.encode(content,"utf-8"));//打印最后拼接成的urllogger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",get url is :"+sb.toString());URL url = new URL(sb.toString());URLConnection conn = url.openConnection();conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));String line = "";String result = "";while ((line=in.readLine())!=null){result += line;}in.close();logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",getSendMessage end");logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",getSendMessage end");return result;}catch(Exception e){logger.error(ERROR_PREFIX+"execute getSendMessage occur error,the exception is:"+e);throw e;}}/*** post方法发送短信* 只发送普通短信* List<String> mobilePhones电话号码* String content短信内容*/public static Object postSendSmsMessage(List<String> mobilePhones,String content) throws Exception{logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",postSendMessage begin");logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",postSendMessage begin");try {//从配置文件中读取发送短信的参数String username = ResourceUtil.getConf("sms.message.username");String password = ResourceUtil.getConf("sms.message.password");String messageUrl = ResourceUtil.getConf("sms.message.post.url");PrintWriter out = null;URL url = new URL(messageUrl);StringBuffer params = new StringBuffer();params.append("username="+username);String passwordN = md5(username+md5(password));params.append("&password="+passwordN);params.append("&mobile=");for(String mp:mobilePhones){params.append(mp+",");}//去掉最后一个逗号params.deleteCharAt(params.length() - 1);params.append("&content="+URLEncoder.encode(content,"utf-8"));URLConnection conn = url.openConnection();conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setDoOutput(true);conn.setDoInput(true);out = new PrintWriter(conn.getOutputStream());out.print(params);out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));String line = "";String result = "";while ((line=in.readLine())!=null){result += line;}out.close();in.close();logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",postSendMessage end");logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+",postSendMessage end");return result;} catch (Exception e) {logger.error(ERROR_PREFIX+"execute postSendMessage occur error,the exception is:"+e);throw e;}public static void main(String[] args) {}}
原创粉丝点击