企业开发常用API-HttpClient

 

package com.live.test.api.core.http;

import static com.live.test.api.core.io.IOUtil.coverInputStremToString;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
 * 
 * @author live
 */
public class HttpClient {
//	private static final Logger log = LoggerFactory.getLogger(HttpClient.class);

	/**
	 * get请求
	 * 
	 * @param urlStr
	 * @return
	 */
	public static String get(String urlStr) {
		System.out.println("请求方式:get,	url:" + urlStr);

		InputStream in = getToInputStream(urlStr);
		if (in == null) {
			return null;
		}

		String r = coverInputStremToString(in);
		System.out.println("result:" + r);
		return r;
	}

	/**
	 * post请求
	 * 
	 * @param urlStr
	 * @return
	 */
	public static String post(String urlStr) {
		return coverInputStremToString(postForInputs(urlStr, null));
	}

	/**
	 * post请求,可携带参数
	 * 
	 * @param urlStr
	 * @param queryString
	 * @return
	 */
	public static String post(String urlStr, String queryString) {
		return coverInputStremToString(postForInputs(urlStr, queryString));
	}

	/**
	 * post 请求
	 * 
	 * @param urlStr
	 * @param queryString
	 * @return
	 */
	private static InputStream postForInputs(String urlStr, String queryString) {
		System.out.println("请求方式:post,	url:" + urlStr + ",	参数:" + queryString);
		URL url = null;
		try {
			url = new URL(urlStr);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		if (url == null) {
			return null;
		}

		InputStream is = null;
		try {
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("POST");
			urlConnection.setConnectTimeout(5000);
			urlConnection.setDoInput(true);
			urlConnection.setDoOutput(true);
			urlConnection.connect();
			OutputStream os = urlConnection.getOutputStream();
			if (queryString != null) {
				os.write(queryString.getBytes());
			}

			return urlConnection.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;
	}

	/**
	 * get 请求
	 * 
	 * @param urlStr
	 * @return
	 */
	public static InputStream getToInputStream(String urlStr) {
//		log.info("get请求开始。url:" + urlStr);
		URL url = null;
		try {
			url = new URL(urlStr);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		if (url == null) {
			return null;
		}

		InputStream is = null;
		try {
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.setConnectTimeout(5000);

			urlConnection.connect();

			InputStream in = urlConnection.getInputStream();
			return in;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;

	}

//	public static void main(String[] args) throws IOException {
//		String urlStr = "http://xxx.png";
//		InputStream r = HttpClient.getInputStream(urlStr);
		System.out.println(r);
//		System.out.println(r.available());
//
//		String pathname = "/opt/11.png";
//		File file = new File(pathname);
//
//		FileUtil.wrriteFile(r, file.getAbsolutePath());
//
//		System.out.println(file.length());
//
//		InputStream is = new FileInputStream(file);
//		byte[] data = new byte[is.available()];
//		is.read(data);
//		is.close();
//		// TODO 使用JDK8 Base64,待验证
//		// return encoder.encode(data);
//		String s = Base64.getEncoder().encodeToString(data);
//		System.out.println(s.length());
//
//		String txt = "/opt/base64.txt";
//		FileUtil.write(txt, s, false);
//		// System.out.println(s);
//	}

	public static void main(String[] args) {
		String urlStr = "https://www.google.com";
		String r = HttpClient.get(urlStr);
		System.out.println(r);
		r = HttpClient.post(urlStr);
		System.out.println(r);
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值