网络请求工具类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<String, String> dataMap) {
		 CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpClientContext context = HttpClientContext.create();
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Set<String> 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;

     }
	 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值