| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package cn.com.lzt.dialogDeal.http;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- 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.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import org.apache.log4j.Logger;
- import com.thoughtworks.xstream.core.util.Base64Encoder;
- /**
- * 单元测试公共方法
- * https
- * @author zbw
- * 2017-12-6
- */
- public class ClientUtil {
-
- static Logger logger=Logger.getLogger(Logger.class);
-
- @SuppressWarnings("resource")
- public static String resultData(String url, Map<String,String> map){
-
- HttpClient httpClient =null;
- try {
- HttpPost post = null;
- httpClient =new SSLClientUtil();
-
- // 创建参数列表
- List<NameValuePair> list = new ArrayList<NameValuePair>();
- post = new HttpPost(url);
- post.setHeader("Authorization",Auth());
- for (String key:map.keySet()) {
- list.add(new BasicNameValuePair(key, map.get(key)));
- }
-
- // url编码格式
- UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
- post.setEntity(urlEncodedFormEntity);
- //System.out.println("POST请求:" + post.getURI());
- // 执行请求
- HttpResponse httpResponse = httpClient.execute(post);
-
- HttpEntity entity = httpResponse.getEntity();
- if (null != entity) {
- String result = EntityUtils.toString(entity);
- System.out.println(result);
- return result;
- }
- } catch (Exception e) {
- logger.error(e);
- return "false";
- }
-
-
- return null;
- }
- @SuppressWarnings("resource")
- public static String doHttpsGet(String url){
- //String url="https://192.168.100.108:2601/iam-storage/asset/selectAssetType?assetTypeName=all&pageNum=1";
-
- HttpClient httpClient =null;
- try {
- HttpGet get = null;
- get = new HttpGet(url);
- get.setHeader("Authorization",Auth());
-
- httpClient =new SSLClientUtil();
- // 执行请求
- HttpResponse httpResponse = httpClient.execute(get);
-
- HttpEntity entity = httpResponse.getEntity();
- if (null != entity) {
- String result = EntityUtils.toString(entity);
- System.out.println(result);
- return result;
- }
- } catch (Exception e) {
- logger.error(e);
- return "false";
- }
-
-
- return null;
- }
- public static String Auth(){
- String username = "admin";
- String password = "123456";
-
- String auth=username+":"+password;
- //byte[] encodeAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("utf-8")));
- String encodeAuth = new Base64Encoder().encode(auth.getBytes(Charset.forName("utf-8")));
- String authHeader = "Basic "+new String(encodeAuth);
-
- //System.out.println(authHeader);
- return authHeader;
- }
- }
|