ClientUtil.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package cn.com.lzt.dialogDeal.http;
  2. import java.nio.charset.Charset;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.NameValuePair;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.apache.http.util.EntityUtils;
  15. import org.apache.log4j.Logger;
  16. import com.thoughtworks.xstream.core.util.Base64Encoder;
  17. /**
  18. * 单元测试公共方法
  19. * https
  20. * @author zbw
  21. * 2017-12-6
  22. */
  23. public class ClientUtil {
  24. static Logger logger=Logger.getLogger(Logger.class);
  25. @SuppressWarnings("resource")
  26. public static String resultData(String url, Map<String,String> map){
  27. HttpClient httpClient =null;
  28. try {
  29. HttpPost post = null;
  30. httpClient =new SSLClientUtil();
  31. // 创建参数列表
  32. List<NameValuePair> list = new ArrayList<NameValuePair>();
  33. post = new HttpPost(url);
  34. post.setHeader("Authorization",Auth());
  35. for (String key:map.keySet()) {
  36. list.add(new BasicNameValuePair(key, map.get(key)));
  37. }
  38. // url编码格式
  39. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
  40. post.setEntity(urlEncodedFormEntity);
  41. //System.out.println("POST请求:" + post.getURI());
  42. // 执行请求
  43. HttpResponse httpResponse = httpClient.execute(post);
  44. HttpEntity entity = httpResponse.getEntity();
  45. if (null != entity) {
  46. String result = EntityUtils.toString(entity);
  47. System.out.println(result);
  48. return result;
  49. }
  50. } catch (Exception e) {
  51. logger.error(e);
  52. return "false";
  53. }
  54. return null;
  55. }
  56. @SuppressWarnings("resource")
  57. public static String doHttpsGet(String url){
  58. //String url="https://192.168.100.108:2601/iam-storage/asset/selectAssetType?assetTypeName=all&pageNum=1";
  59. HttpClient httpClient =null;
  60. try {
  61. HttpGet get = null;
  62. get = new HttpGet(url);
  63. get.setHeader("Authorization",Auth());
  64. httpClient =new SSLClientUtil();
  65. // 执行请求
  66. HttpResponse httpResponse = httpClient.execute(get);
  67. HttpEntity entity = httpResponse.getEntity();
  68. if (null != entity) {
  69. String result = EntityUtils.toString(entity);
  70. System.out.println(result);
  71. return result;
  72. }
  73. } catch (Exception e) {
  74. logger.error(e);
  75. return "false";
  76. }
  77. return null;
  78. }
  79. public static String Auth(){
  80. String username = "admin";
  81. String password = "123456";
  82. String auth=username+":"+password;
  83. //byte[] encodeAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("utf-8")));
  84. String encodeAuth = new Base64Encoder().encode(auth.getBytes(Charset.forName("utf-8")));
  85. String authHeader = "Basic "+new String(encodeAuth);
  86. //System.out.println(authHeader);
  87. return authHeader;
  88. }
  89. }