RestUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.persagy.util;
  2. import cn.hutool.core.thread.ThreadUtil;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.NameValuePair;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.HttpGet;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.entity.ByteArrayEntity;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.message.BasicNameValuePair;
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.InputStream;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. public class RestUtil {
  17. private static RestUtil instance = new RestUtil();
  18. public static RestUtil Instance() {
  19. return instance;
  20. }
  21. public static RestUtil NewInstance() {
  22. return new RestUtil();
  23. }
  24. private RestUtil() {
  25. }
  26. DefaultHttpClient client = new DefaultHttpClient();
  27. public synchronized String Get(String url) throws Exception {
  28. try {
  29. HttpGet httpget = new HttpGet(url);
  30. HttpResponse response = client.execute(httpget);
  31. InputStream is = response.getEntity().getContent();
  32. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  33. int i = -1;
  34. while ((i = is.read()) != -1) {
  35. baos.write(i);
  36. }
  37. return baos.toString("UTF-8");
  38. } catch (Exception e) {
  39. ThreadUtil.safeSleep(1000L * 5);
  40. client = new DefaultHttpClient();
  41. throw e;
  42. }
  43. }
  44. public synchronized String PostForm(String url, String paramName, String jsonString) throws Exception {
  45. try {
  46. HttpPost httpost = new HttpPost(url);
  47. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  48. nvps.add(new BasicNameValuePair(paramName, jsonString));
  49. httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
  50. HttpResponse response = client.execute(httpost);
  51. InputStream is = response.getEntity().getContent();
  52. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  53. int i = -1;
  54. while ((i = is.read()) != -1) {
  55. baos.write(i);
  56. }
  57. return baos.toString("UTF-8");
  58. } catch (Exception e) {
  59. ThreadUtil.safeSleep(1000L * 5);
  60. client = new DefaultHttpClient();
  61. throw e;
  62. }
  63. }
  64. public synchronized String PostRaw(String url, String jsonString) throws Exception {
  65. try {
  66. HttpPost httpost = new HttpPost(url);
  67. httpost.setEntity(new StringEntity(jsonString, "UTF-8"));
  68. HttpResponse response = client.execute(httpost);
  69. InputStream is = response.getEntity().getContent();
  70. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  71. int i = -1;
  72. while ((i = is.read()) != -1) {
  73. baos.write(i);
  74. }
  75. return baos.toString("UTF-8");
  76. } catch (Exception e) {
  77. ThreadUtil.safeSleep(1000L * 5);
  78. client = new DefaultHttpClient();
  79. throw e;
  80. }
  81. }
  82. public synchronized String PostRaw(String url, byte[] jsonString) throws Exception {
  83. try {
  84. HttpPost httpost = new HttpPost(url);
  85. httpost.setEntity(new ByteArrayEntity(jsonString));
  86. HttpResponse response = client.execute(httpost);
  87. InputStream is = response.getEntity().getContent();
  88. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  89. int i = -1;
  90. while ((i = is.read()) != -1) {
  91. baos.write(i);
  92. }
  93. return baos.toString("UTF-8");
  94. } catch (Exception e) {
  95. ThreadUtil.safeSleep(1000L * 5);
  96. client = new DefaultHttpClient();
  97. throw e;
  98. }
  99. }
  100. }