封装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;

/**
 * 

* 2012-03-01
* 软维提供的JAVA接口信息(短信,彩信)调用API
* ----------基础访问方法------------- *

* * @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; } /** *

* POST方法 *

* * @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(); } }

你可能感兴趣的:(Java学习笔记)