|
@@ -4,13 +4,118 @@ import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
-
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
public class HttpUtil {
|
|
|
- public static JSONObject httpGet(String url) {
|
|
|
- HttpResponse response = HttpRequest.get(url).execute();
|
|
|
- String json = response.charset("utf-8").body();
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(json);
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
+ public static JSONObject httpGet(String url) {
|
|
|
+ HttpResponse response = HttpRequest.get(url).execute();
|
|
|
+ String json = response.charset("utf-8").body();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(json);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+ public static String dourl(String url,Object clzz){
|
|
|
+ try {
|
|
|
+ String jsonString = JSON.toJSONString(clzz);
|
|
|
+ URL url1 = new URL(url);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
|
|
|
+ //设置允许输出
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ //设置允许输入
|
|
|
+ conn.setDoInput(true);
|
|
|
+ //设置不用缓存
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
|
|
|
+ //设置传递方式
|
|
|
+ conn.setRequestProperty("contentType", "application/json");
|
|
|
+ // 设置维持长连接
|
|
|
+ conn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ // 设置文件字符集:
|
|
|
+ conn.setRequestProperty("Charset", "UTF-8");
|
|
|
+ //开始请求
|
|
|
+ byte[] bytes = jsonString.toString().getBytes();
|
|
|
+ //写流
|
|
|
+ OutputStream stream = conn.getOutputStream();
|
|
|
+ stream.write(bytes);
|
|
|
+ stream.flush();
|
|
|
+ stream.close();
|
|
|
+ int resultCode=conn.getResponseCode();
|
|
|
+ if(conn.getResponseCode()==200){
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
+ byte[] bytes1 = new byte[inputStream.available()];
|
|
|
+ inputStream.read(bytes1);
|
|
|
+ //转字符串
|
|
|
+ String s = new String(bytes);
|
|
|
+ System.out.println(s);
|
|
|
+ return s;
|
|
|
+ }else {
|
|
|
+ //获取响应内容
|
|
|
+ int code = conn.getResponseCode();
|
|
|
+ String responseMessage = conn.getResponseMessage();
|
|
|
+ System.out.println(code);
|
|
|
+ String s = String.valueOf(code);
|
|
|
+ return "响应状态码是:"+s+"响应内容是:======="+responseMessage;
|
|
|
+ }
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public static String doPost(String url, Object myclass) {
|
|
|
+ CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+ HttpPost posturl = new HttpPost(url);
|
|
|
+ String result = null;
|
|
|
+ String jsonSting = JSON.toJSONString(myclass);
|
|
|
+ StringEntity entity = new StringEntity(jsonSting, "UTF-8");
|
|
|
+ posturl.setEntity(entity);
|
|
|
+ posturl.setHeader("Content-Type", "application/json;charset=utf8");
|
|
|
+ // 响应模型
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ // 由客户端执行(发送)Post请求
|
|
|
+ response = httpClient.execute(posturl);
|
|
|
+ // 从响应模型中获取响应实体
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+
|
|
|
+ System.out.println("响应状态为:" + response.getStatusLine());
|
|
|
+ if (responseEntity != null) {
|
|
|
+ System.out.println("响应内容长度为:" + responseEntity.getContentLength());
|
|
|
+ System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
|
|
|
+ System.out.println(response);
|
|
|
+// return EntityUtils.toString(responseEntity);
|
|
|
+ }
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ // 释放资源
|
|
|
+ if (httpClient != null) {
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|