HttpRequest.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package cn.com.lzt.dialogDeal.http;
  2. import java.io.Closeable;
  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.entity.UrlEncodedFormEntity;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.apache.http.util.EntityUtils;
  17. /**
  18. * http
  19. * @author zbw
  20. * 2017-12-6
  21. */
  22. public class HttpRequest {
  23. public static int count=0;
  24. public static String doPost(String url, Map<String,String> map) {
  25. CloseableHttpClient httpClient = HttpClients.createDefault();
  26. try{
  27. //HttpPost post = null;
  28. HttpPost post =null;
  29. // 创建参数列表
  30. List<NameValuePair> list = new ArrayList<NameValuePair>();
  31. post = new HttpPost(url);
  32. for (String key:map.keySet()) {
  33. list.add(new BasicNameValuePair(key, map.get(key)));
  34. }
  35. /*list.add(new BasicNameValuePair("assetType", "all"));
  36. list.add(new BasicNameValuePair("pageNum", "1"));
  37. list.add(new BasicNameValuePair("username", "admin"));
  38. list.add(new BasicNameValuePair("password", "89a996b60fa0e54bf9aa115e5dad32677a6b1220069dc9c7e16605a6"));*/
  39. //post.setHeader("Authorization", "bb");
  40. //post.setHeader("Authorization1", "bb");
  41. // url编码格式
  42. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
  43. post.setEntity(urlEncodedFormEntity);
  44. // 执行请求
  45. CloseableHttpResponse httpResponse = httpClient.execute(post);
  46. try{
  47. HttpEntity entity = httpResponse.getEntity();
  48. if (null != entity) {
  49. String result = EntityUtils.toString(entity);
  50. System.out.println(result);
  51. if(result.equals("failed")){
  52. count++;
  53. if(count<5){
  54. doPost(url, map);
  55. }
  56. }
  57. return result;
  58. }
  59. } finally {
  60. httpResponse.close();
  61. }
  62. } catch (Exception e){
  63. e.printStackTrace();
  64. }
  65. return url;
  66. }
  67. public static String doGet(String url) {
  68. CloseableHttpClient httpClient = HttpClients.createDefault();
  69. try{
  70. //HttpPost post = null;
  71. HttpGet hGet =null;
  72. // 创建参数列表
  73. // List<NameValuePair> list = new ArrayList<NameValuePair>();
  74. hGet = new HttpGet(url);
  75. // for (String key:map.keySet()) {
  76. // list.add(new BasicNameValuePair(key, map.get(key)));
  77. // }
  78. /*list.add(new BasicNameValuePair("assetType", "all"));
  79. list.add(new BasicNameValuePair("pageNum", "1"));
  80. list.add(new BasicNameValuePair("username", "admin"));
  81. list.add(new BasicNameValuePair("password", "89a996b60fa0e54bf9aa115e5dad32677a6b1220069dc9c7e16605a6"));*/
  82. //post.setHeader("Authorization", "bb");
  83. //post.setHeader("Authorization1", "bb");
  84. // url编码格式
  85. //UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( "UTF-8");
  86. //post.setEntity(urlEncodedFormEntity);
  87. // 执行请求
  88. //CloseableHttpResponse httpResponse = httpClient.execute(post);
  89. HttpResponse httpResponse=httpClient.execute(hGet);
  90. try{
  91. HttpEntity entity = httpResponse.getEntity();
  92. if (null != entity) {
  93. String result = EntityUtils.toString(entity);
  94. System.out.println(result);
  95. return result;
  96. }
  97. } finally {
  98. ((Closeable) httpResponse).close();
  99. }
  100. } catch (Exception e){
  101. e.printStackTrace();
  102. }
  103. return null;
  104. }
  105. }