封装Http工具类

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值