码迷,mamicode.com
首页 > 其他好文 > 详细

又一个短信发送类

时间:2015-03-17 12:44:23      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;


import net.sf.json.JSONObject;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * http请求工具类
 */
public class HttpUtil {
private static Log log = LogFactory.getLog(FormUtil.class);


/**
* 发送请求 ,返回请求结果
*/
public static String http(String url, Map<String, String> params) {
URL u = null;
HttpURLConnection con = null;
StringBuffer sb = new StringBuffer();
if (params != null) {
log.info("短信接口参数发送开始");
for (Entry<String, String> e : params.entrySet()) {
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
sb.append("&");
log.info(e.getKey() + "=============" + e.getValue());
}
log.info("短信接口参数发送结束");
sb.deleteCharAt(sb.length() - 1);
}
// 发送请求
try {
u = new URL(url);
con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GB2312");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "GB2312");
osw.write(sb.toString());
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
}


// 读取返回内容
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "GB2312"));
String temp;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
}

又一个短信发送类

标签:

原文地址:http://my.oschina.net/u/1020166/blog/387847

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!