zoukankan      html  css  js  c++  java
  • httpclient4封装类

    package com.kcb.common.util;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.NameValuePair;
    import org.apache.http.ParseException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.params.ClientPNames;
    import org.apache.http.client.params.CookiePolicy;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;
    
    /**
     * @author  zhf
     * @version 创建时间:2013-6-28 下午3:42:46 httpclient4工具类
     */
    public class HttpClient4Util {
    
    	private static HttpClient4Util HTTP_CLIENT4_UTIL = null;
    
    	/**
    	 * 单例获取
    	 * 
    	 * @return
    	 */
    	public HttpClient4Util getInstanct() {
    		if (HTTP_CLIENT4_UTIL == null) {
    			HTTP_CLIENT4_UTIL = new HttpClient4Util();
    		}
    		return HTTP_CLIENT4_UTIL;
    	}
    
    	private static HttpClient httpclient = new DefaultHttpClient();
    
    	static {
    		// 设置Cookie的兼容性
    		httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    		httpclient.getConnectionManager().closeIdleConnections(10, TimeUnit.SECONDS);
    
    	}
    
    	/**
    	 * 调用请求接口
    	 * 
    	 * @param urlstr
    	 * @return
    	 */
    	public static String get(String urlstr) {
    		String html = null;
    		try {
    			html = get(urlstr, 0);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return html;
    	}
    
    	/**
    	 * get 请求5次
    	 * 
    	 * @param urlstr
    	 * @return html
    	 * @throws Exception
    	 */
    	public static String get(String urlstr, int count) throws Exception {
    		String html = null;
    		count++;
    		if (count > 5) {
    			throw new Exception();// 这里可以抛出请求超时异常
    		}
    		URL url = new URL(urlstr);
    		URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
    		HttpGet httpget = new HttpGet(uri);
    		HttpResponse response = httpclient.execute(httpget);
    		HttpEntity httpEntity = response.getEntity();
    		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && httpEntity != null) {
    			html = EntityUtils.toString(httpEntity, "utf-8");
    			httpEntity.consumeContent();
    		} else {
    			html = get(urlstr, count);
    		}
    
    		return html;
    	}
    
    	/**
    	 * 获取文件
    	 * 
    	 * @param urlstr
    	 * @param file
    	 */
    	public static void get(String urlstr, String file) {
    		try {
    			URL url = new URL(urlstr);
    			URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
    			HttpGet httpGet = new HttpGet(uri);
    			HttpResponse response = httpclient.execute(httpGet);
    			HttpEntity httpEntity = response.getEntity();
    
    			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && httpEntity != null) {
    				byte[] b = EntityUtils.toByteArray(httpEntity);
    				FileUtils.writeByteArrayToFile(new File(file), b);
    			}
    			httpEntity.consumeContent();
    			// EntityUtils.consume(httpEntity);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
    
    	/**
    	 * post请求
    	 * 
    	 * @param url
    	 * @param map
    	 * @return
    	 */
    	public String post(String url, Map<String, String> map) {
    		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    		for (String key : map.keySet()) {
    			NameValuePair nvp = new BasicNameValuePair(key, map.get(key));
    			nvps.add(nvp);
    		}
    
    		String html = null;
    		try {
    			HttpPost httpPost = new HttpPost(url);
    			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    			HttpResponse response = httpclient.execute(httpPost);
    
    			HttpEntity httpEntity = response.getEntity();
    			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && httpEntity != null) {
    				html = EntityUtils.toString(httpEntity, "utf-8");
    				// EntityUtils.consume(httpEntity);
    				httpEntity.consumeContent();
    
    			}
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return html;
    	}
    
    }
    

      需要添加httpclient和相应的common扎包。

  • 相关阅读:
    MAC下通过改apache配置文件切换php多版本的方法
    mac配置php7运行环境
    VS Code 配置 C/C++ 环境(转)
    go语言中goroute使用:=遇到的坑
    python中执行shell的两种方法总结
    对于pycharm和vscode下,从外部复制文本内容为python字符串内容是会自动加u202a解决办法
    在macOS下正确配置 VS Code 使用 virtualenv 里的 python 环境参数
    github上传时出现error: src refspec master does not match any解决办法
    VSCode tasks.json中的各种替换变量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等
    Sonar入门(四):Eclipse集成Sonar
  • 原文地址:https://www.cnblogs.com/cxyzl/p/3165205.html
Copyright © 2011-2022 走看看