JAVA 기반 HTTP 요청 도구 클래스

9509 단어 웹 백 엔 드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 *     HTTP          
* params url encode, url encode, * @Description: HTTP * @author lushuifa * @date 2016 11 18 11:21:05 */ public class HttpUtil { /** * logger */ private static Logger logger = Logger.getLogger(HttpUtil.class); /** * Http method * */ private static enum HttpMethod { POST,DELETE,GET,PUT,HEAD; }; @SuppressWarnings({ "unchecked", "rawtypes" }) private static String invokeUrl(String url, Map params, Map headers, int connectTimeout, int readTimeout, String encoding, HttpMethod method){ // StringBuilder paramsStr = null; if(params != null){ paramsStr = new StringBuilder(); Set entries = params.entrySet(); for(Map.Entry entry:entries){ String value = (entry.getValue()!=null)?(String.valueOf(entry.getValue())):""; paramsStr.append(entry.getKey() + "=" + value + "&"); } // POST OutputStream( form ) if(method != HttpMethod.POST){ url += "?"+paramsStr.toString(); } } URL uUrl = null; HttpURLConnection conn = null; BufferedWriter out = null; BufferedReader in = null; try { // uUrl = new URL(url); conn = (HttpURLConnection) uUrl.openConnection(); conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); conn.setRequestMethod(method.toString()); conn.setDoOutput(true); conn.setDoInput(true); // conn.setConnectTimeout(connectTimeout); // conn.setReadTimeout(readTimeout); // header if(headers != null && headers.size() > 0){ Set headerSet = headers.keySet(); for(String key:headerSet){ conn.setRequestProperty(key, headers.get(key)); } } if(paramsStr != null && method == HttpMethod.POST){ // out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),encoding)); out.write(paramsStr.toString()); out.flush(); } // StringBuilder result = new StringBuilder(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(),encoding)); if(in != null){ String line = ""; while ((line = in.readLine()) != null) { result.append(line); } } return result.toString(); } catch (Exception e) { logger.error(" ["+url+"] ! URL:"+url+", :"+params,e); // , http try { byte[] buf = new byte[100]; InputStream es = conn.getErrorStream(); if(es != null){ while (es.read(buf) > 0) {;} es.close(); } } catch (Exception e1) { e1.printStackTrace(); } } finally { try { if (out!=null) { out.close(); } }catch (Exception e) { e.printStackTrace(); } try { if (in !=null) { in.close(); } }catch (Exception e) { e.printStackTrace(); } // if (conn != null){ conn.disconnect(); } } return null; } /** * POST Http , “ ”
* :Http POST body * @param url ( url , params null) * @param params * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String post(String url, Map params, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.POST); } /** * POST Http , “ ”
* :Http POST body * @param url ( url , params null) * @param params * @param headers * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String post(String url, Map params, Map headers, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.POST); } /** * GET Http , “ ” * @param url ( url , params null) * @param params * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String get(String url, Map params, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.GET); } /** * GET Http , “ ” * @param url ( url , params null) * @param params * @param headers * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String get(String url, Map params, Map headers,int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.GET); } /** * PUT Http , “ ”
* :PUT url body, ,Resin 8K * @param url ( url , params null) * @param params * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String put(String url, Map params, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.PUT); } /** * PUT Http , “ ”
* :PUT url body, ,Resin 8K * @param url ( url , params null) * @param params * @param headers * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String put(String url, Map params, Map headers,int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.PUT); } /** * DELETE Http , “ ” * @param url ( url , params null) * @param params * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String delete(String url, Map params, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.DELETE); } /** * DELETE Http , “ ” * @param url ( url , params null) * @param params * @param headers * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String delete(String url, Map params, Map headers, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.DELETE); } /** * HEAD Http , GET
* GET , , message body , * @param url ( url , params null) * @param params * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String head(String url, Map params, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.HEAD); } /** * HEAD Http , GET
* GET , , message body , * @param url ( url , params null) * @param params * @param headers * @param connectTimeout ( ms) * @param readTimeout ( ms) * @param charset ( “utf-8”) * @return */ public static String head(String url, Map params, Map headers, int connectTimeout, int readTimeout, String charset){ return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.HEAD); } public static void main(String[] args) { //Map params = new HashMap(); //params.put("phoneNo", " "); //String str = HttpUtil.get("http://localhost:9092/elis_smp_als_dmz/do/app/activitySupport/demo", params, 3000, 3000, "UTF-8"); //System.out.println(str); } }

좋은 웹페이지 즐겨찾기