|
@@ -4,19 +4,32 @@ import java.io.ByteArrayOutputStream;
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
-import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
|
public class HttpClientUtil {
|
|
|
|
|
|
- private static DefaultHttpClient client = new DefaultHttpClient();
|
|
|
+ private static final HttpClient client = HttpClients.createDefault();
|
|
|
|
|
|
- public synchronized static String post(String post_url, String post_body) throws Exception {
|
|
|
- HttpPost httpost = new HttpPost(post_url);
|
|
|
- StringEntity entity = new StringEntity(post_body.toString(), "UTF-8");
|
|
|
- entity.setContentType("application/json");
|
|
|
+ public synchronized static String post(String url, String content) throws Exception {
|
|
|
+ String result = post(url, content, 300000);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized static String post(String url, String content, Integer timeout) throws Exception {
|
|
|
+ HttpPost httpost = new HttpPost(url);
|
|
|
+ if (timeout != null) {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
|
+ .setSocketTimeout(timeout).build();
|
|
|
+ httpost.setConfig(requestConfig);
|
|
|
+ }
|
|
|
+ StringEntity entity = new StringEntity(content, "UTF-8");
|
|
|
httpost.setEntity(entity);
|
|
|
+ entity.setContentType("application/json");
|
|
|
HttpResponse httpResponse = client.execute(httpost);
|
|
|
InputStream is = httpResponse.getEntity().getContent();
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
@@ -24,9 +37,33 @@ public class HttpClientUtil {
|
|
|
while ((b = is.read()) != -1) {
|
|
|
baos.write(b);
|
|
|
}
|
|
|
- String result = baos.toString("UTF-8");
|
|
|
baos.close();
|
|
|
is.close();
|
|
|
+
|
|
|
+ String result = baos.toString("UTF-8");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized static String get(String url, Integer timeout) throws Exception {
|
|
|
+ HttpGet httpget = new HttpGet(url);
|
|
|
+ if (timeout != null) {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
|
+ .setSocketTimeout(timeout).build();
|
|
|
+ httpget.setConfig(requestConfig);
|
|
|
+ }
|
|
|
+ // httpget.setHeader(new BasicHeader("Content-type", "text/plain;charset=utf-8"));
|
|
|
+ // httpget.setHeader(new BasicHeader("Content-type", "application/json;charset=utf-8"));
|
|
|
+ HttpResponse response = client.execute(httpget);
|
|
|
+ InputStream is = response.getEntity().getContent();
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ int i = -1;
|
|
|
+ while ((i = is.read()) != -1) {
|
|
|
+ os.write(i);
|
|
|
+ }
|
|
|
+ is.close();
|
|
|
+ os.close();
|
|
|
+
|
|
|
+ String result = os.toString("UTF-8");
|
|
|
return result;
|
|
|
}
|
|
|
}
|