package com.persagy.util; import cn.hutool.core.thread.ThreadUtil; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; 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.entity.ByteArrayEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class RestUtil { private static RestUtil instance = new RestUtil(); public static RestUtil Instance() { return instance; } public static RestUtil NewInstance() { return new RestUtil(); } private RestUtil() { } DefaultHttpClient client = new DefaultHttpClient(); public synchronized String Get(String url) throws Exception { try { HttpGet httpget = new HttpGet(url); HttpResponse response = client.execute(httpget); InputStream is = response.getEntity().getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.toString("UTF-8"); } catch (Exception e) { ThreadUtil.safeSleep(1000L * 5); client = new DefaultHttpClient(); throw e; } } public synchronized String PostForm(String url, String paramName, String jsonString) throws Exception { try { HttpPost httpost = new HttpPost(url); List nvps = new ArrayList(); nvps.add(new BasicNameValuePair(paramName, jsonString)); httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); HttpResponse response = client.execute(httpost); InputStream is = response.getEntity().getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.toString("UTF-8"); } catch (Exception e) { ThreadUtil.safeSleep(1000L * 5); client = new DefaultHttpClient(); throw e; } } public synchronized String PostRaw(String url, String jsonString) throws Exception { try { HttpPost httpost = new HttpPost(url); httpost.setEntity(new StringEntity(jsonString, "UTF-8")); HttpResponse response = client.execute(httpost); InputStream is = response.getEntity().getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.toString("UTF-8"); } catch (Exception e) { ThreadUtil.safeSleep(1000L * 5); client = new DefaultHttpClient(); throw e; } } public synchronized String PostRaw(String url, byte[] jsonString) throws Exception { try { HttpPost httpost = new HttpPost(url); httpost.setEntity(new ByteArrayEntity(jsonString)); HttpResponse response = client.execute(httpost); InputStream is = response.getEntity().getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.toString("UTF-8"); } catch (Exception e) { ThreadUtil.safeSleep(1000L * 5); client = new DefaultHttpClient(); throw e; } } }