网络请求工具类HttpUtil

package com.szjiuli.business.commom.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;



public class HttpUtil {
	
	 public static final String GET = "GET";
	 public static final String POST = "POST";
	 public static final String DELETE = "DELETE";
	 
	 
	 public static String sendGet(String url,String param) {  
	        String result = "";  
	        BufferedReader in = null;  
	        try {  
	            String urlName = url + "?" + param;  
	            System.out.println("url:"+urlName);
	            URL realUrl = new URL(urlName);  
	            URLConnection conn = realUrl.openConnection();  
	            conn.setConnectTimeout(10*1000);
	            conn.setRequestProperty("accept", "*/*");  
	            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");   
	            conn.connect();  

	            // 定义BufferedReader输入流来读取URL的响应  
	            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));  
	            String line;  
	            while ((line = in.readLine()) != null) {  
	                result += line;  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	            return null;
	        } finally {  
	            try {  
	                if (in != null) {  
	                    in.close();  
	                }  
	            } catch (IOException ex) {  
	                ex.printStackTrace();  
	            }  
	        }  
	        return result;  
	    } 
	  
	 public static String sendPost(String url, Map dataMap) {
		 CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpClientContext context = HttpClientContext.create();
			HttpPost httpPost = new HttpPost(url);
			List nvps = new ArrayList();
			Set keys = dataMap.keySet();
			for (String key : keys) {
				nvps.add(new BasicNameValuePair(key, (String) dataMap.get(key)));
			}
	        String result = null;
			try {
				httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36 CoolNovo/2.0.9.19");
				httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
				CloseableHttpResponse response = httpclient.execute(httpPost, context);
				result = "";
				StringBuffer buffer = new StringBuffer();
				BufferedReader reader = null;
				// 定义BufferedReader输入流来读取URL的响应
				reader = new BufferedReader(new InputStreamReader( 
						 response.getEntity().getContent(), "GBK"));
				response.setHeader("content-type", "application/pdf");
				Header[] hs =response.getAllHeaders();  
				    for (Header header : hs) {  
				        System.out.println(header.toString());  
				    }  
				String line = null;
				while ((line = reader.readLine()) != null) {
					buffer.append(line);
				}
				result = buffer.toString();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(result);
			return result;

     }
	 
}

 

你可能感兴趣的:(java)