封装Http工具类
来源:互联网 发布:mongodb java api 编辑:程序博客网 时间:2024/06/08 18:05
package com.micropoplar.gm.common.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;/** * <p> * <date>2012-03-01</date><br/> * <span>软维提供的JAVA接口信息(短信,彩信)调用API</span><br/> * <span>----------基础访问方法-------------</span> * </p> * * @author LIP * @version 1.0.1 */public class SmsClientAccessTool { private static SmsClientAccessTool smsClientToolInstance; /** * 采用单列方式来访问操作 * * @return */ public static synchronized SmsClientAccessTool getInstance() { if (smsClientToolInstance == null) { smsClientToolInstance = new SmsClientAccessTool(); } return smsClientToolInstance; } /** * <p> * POST方法 * </p> * * @param sendUrl :访问URL * @param :参数串 * @param backEncodType :返回的编码 * @return */ public String doAccessHTTPPost(String sendUrl, String sendParam, String backEncodType) { StringBuffer receive = new StringBuffer(); BufferedWriter wr = null; try { if (backEncodType == null || backEncodType.equals("")) { backEncodType = "UTF-8"; } URL url = new URL(sendUrl); HttpURLConnection URLConn = (HttpURLConnection) url.openConnection(); URLConn.setDoOutput(true);// 设置是否向httpUrlConnection输出 因为是post请求 参数需要放在http正文里面 所以需要设置为true 默认是为false URLConn.setDoInput(true); // 设置是否从httpUrlConnection 读入 默认情况下是true ((HttpURLConnection) URLConn).setRequestMethod("POST"); // 设置请求的方法 默认是GET 方法 URLConn.setUseCaches(false); // 设置是否使用缓存 URLConn.setAllowUserInteraction(true); // 设置 HttpURLConnection.setFollowRedirects(true); URLConn.setInstanceFollowRedirects(true); // 设置请求的参数类型 设置了默认的表单类型 URLConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); URLConn.setRequestProperty("Content-Length", String.valueOf(sendParam.getBytes().length)); // 写数据与发送数据========== //此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法, // 所以在开发中不调用上述的connect()也可以)。 //OutputStream outStrm = httpUrlConnection.getOutputStream(); DataOutputStream dos = new DataOutputStream(URLConn.getOutputStream()); // 相当于是连接http dos.writeBytes(sendParam); // 实际发送数据的代码片段在这里 BufferedReader rd = new BufferedReader(new InputStreamReader(URLConn.getInputStream(), backEncodType)); String line; while ((line = rd.readLine()) != null) { receive.append(line).append("\r\n"); } rd.close(); } catch (java.io.IOException e) { receive.append("访问产生了异常-->").append(e.getMessage()); e.printStackTrace(); } finally { if (wr != null) { try { wr.close(); } catch (IOException ex) { ex.printStackTrace(); } wr = null; } } return receive.toString(); } public String doAccessHTTPGet(String sendUrl, String backEncodType) { StringBuffer receive = new StringBuffer(); BufferedReader in = null; try { if (backEncodType == null || backEncodType.equals("")) { backEncodType = "UTF-8"; } URL url = new URL(sendUrl); HttpURLConnection URLConn = (HttpURLConnection) url.openConnection(); URLConn.setDoInput(true); URLConn.setDoOutput(true); URLConn.connect(); URLConn.getOutputStream().flush(); in = new BufferedReader(new InputStreamReader(URLConn.getInputStream(), backEncodType)); String line; while ((line = in.readLine()) != null) { receive.append(line).append("\r\n"); } } catch (IOException e) { receive.append("访问产生了异常-->").append(e.getMessage()); e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (java.io.IOException ex) { ex.printStackTrace(); } in = null; } } return receive.toString(); }}
阅读全文
1 0
- 封装Http工具类
- Http封装请求工具类
- HTTP封装类 工具类 For Android
- Android 封装http请求的工具类
- java实现Http工具类的封装
- Android 封装http请求的工具类
- Android 封装http请求的工具类
- 【Cocos2d-x Lua】http工具类封装
- HTTP请求封装Java工具类
- Android 封装http请求的工具类
- 获取http接口内容的封装工具类
- 封装http操作类
- jdbc封装工具类
- Calendar封装工具类
- XML工具封装类
- JDBC封装工具类
- CoreData封装工具类
- 缓存工具类封装
- JDBC-实现Java代码连接数据库
- dc文件转换.lib->.db
- 前端入门学习笔记(1)--html部分
- 关于studio 3.0编译Android 项目出现 aapt2解决方式(知心版)
- 存储器层次结构2
- 封装Http工具类
- 机器学习的大局:用神经网络和TensorFlow分类文本
- Django框架学习笔记(23.中间件结合实例介绍)
- Maven 学习
- 基于Oauth 2.0的一个登录注册demo
- 安装IDEA
- redis client
- webpack+babel+react搭建
- 每秒处理10万高并发订单的某集团支付系统架构分享