123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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<NameValuePair> nvps = new ArrayList<NameValuePair>();
- 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;
- }
- }
- }
|