/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.http;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.util.FileCopyUtils;

/**
 * @author 田平 create 2018年9月12日下午12:54:34
 */
public class HttpClientUtilDemo {
	public static void main(String[] args) throws Exception {
		 //testBA();
		 testStr();
		// testStr1();
		// testTimeout();
		// testHeader();
		// testBodyString();
		// testForm();
		// testForm1();
		// testFormFile1();
		// testBodyParam();
	}

	/**
	 * 测试获取二进制文件
	 */
	public static void testBA() throws Exception {
		String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1537337608&di=b1c391de734aa27a1c013e856c2f0f74&imgtype=jpg&er=1&src=http%3A%2F%2Fs9.sinaimg.cn%2Fmw690%2F0024zh2Bzy7cIbc0cIM68%26amp%3B690";
		byte[] imageBA = HttpClientUtil.requestBA(url, null, "GET");
		FileCopyUtils.copy(imageBA, new File("d:\\x.jpg"));
	}

	/**
	 * 返回字符串
	 */
	public static void testStr() {
		String url = "http://localhost:11000/TestController/getHello";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", "abcd"));
		String str = HttpClientUtil.requestString(url, params, "GET");
		System.out.println(str);
	}

	/**
	 * 添加url参数
	 */
	public static void testStr1() {
		String url = "http://localhost:10001/TestController/getValue6";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", "abcd"));
		params.add(new BasicNameValuePair("password", "中国"));
		String str = HttpClientUtil.requestString(url, params, "POST");
		System.out.println(str);
	}

	/**
	 * 设置超时时间
	 */
	public static void testTimeout() {
		String url = "http://localhost:10001/TestController/getValue1";
		String str = HttpClientUtil.requestString(url, null, "GET", HttpClientUtil.getSockConf(2000));
		System.out.println(str);
	}

	/**
	 * 通过body传字符串
	 */
	public static void testBodyString() {
		StringEntity entity = new StringEntity("abcdefg中国", Consts.UTF_8);
		String url = "http://localhost:10001/TestController/getValue9";
		String str = HttpClientUtil.reqStrByEntity(url, entity, "POST");
		System.out.println(str);
	}

	/**
	 * 添加basic auth header
	 */
	public static void testHeader() {
		List<Header> headers = new ArrayList<Header>();
		headers.add(HttpClientUtil.getBasicAuth("micro", "micro,."));
		String url = "http://localhost:8888/gateway/local";
		String str = HttpClientUtil.requestString(url, null, "GET", headers);
		System.out.println(str);
	}

	/**
	 * form方式提交
	 */
	public static void testForm() {
		MultipartEntityBuilder mb = MultipartEntityBuilder.create();
		mb.addTextBody("username", "1234", ContentType.create("text/plain", Consts.UTF_8));
		mb.addTextBody("password", "中国", ContentType.create("text/plain", Consts.UTF_8));
		String url = "http://localhost:10001/TestController/getValue6";
		String str = HttpClientUtil.reqStrByEntity(url, mb.build(), "POST");
		System.out.println(str);
	}

	/**
	 * form方式提交(对象接收参数)
	 */
	public static void testForm1() {
		MultipartEntityBuilder mb = MultipartEntityBuilder.create();
		mb.addTextBody("username", "abcd", ContentType.create("text/plain", Consts.UTF_8));
		mb.addTextBody("password", "中国", ContentType.create("text/plain", Consts.UTF_8));
		String url = "http://localhost:10001/TestController/getValue8";
		String str = HttpClientUtil.reqStrByEntity(url, mb.build(), "POST");
		System.out.println(str);
	}

	/**
	 * 通过form方式提交，上传文件
	 */
	public static void testFormFile1() {
		MultipartEntityBuilder mb = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(Consts.UTF_8);
		mb.addTextBody("username", "abcdFF", ContentType.APPLICATION_JSON);
		mb.addTextBody("password", "1234RRRRRRRRRRR!@#$%^&*()_中国", ContentType.APPLICATION_JSON);
		mb.addPart("file", new FileBody(new File("E:/baidu-cloud/Desk-Files/保险.txt")));
		mb.addPart("file", new FileBody(new File("E:/baidu-cloud/Desk-Files/铁塔.sql")));
		String url = "http://localhost:10001/TestController/getValue7";
		String str = HttpClientUtil.reqStrByEntity(url, mb.build(), "POST");
		System.out.println(str);
	}

	/**
	 * 通过body传参数
	 */
	public static void testBodyParam() {
		String url = "http://localhost:10001/TestController/getValue10";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", "abcd"));
		params.add(new BasicNameValuePair("password", "中国人"));
		HttpEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
		String str = HttpClientUtil.reqStrByEntity(url, entity, "POST");
		System.out.println(str);

	}

}
