|
@@ -0,0 +1,126 @@
|
|
|
+package com.steerinfo.dil.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.ParseException;
|
|
|
+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.HttpClients;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.apache.http.protocol.HTTP;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+public class HTTPRequestUtils {
|
|
|
+
|
|
|
+ * 发送post请求
|
|
|
+ * @param url 路径
|
|
|
+ * @param json 参数(json类型)
|
|
|
+ * @param encoding 编码格式
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String send(String url, String json, String encoding) throws ParseException, IOException {
|
|
|
+ String body = "";
|
|
|
+
|
|
|
+
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+
|
|
|
+
|
|
|
+ StringEntity s = new StringEntity(json.toString(), "utf-8");
|
|
|
+ s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
|
|
|
+
|
|
|
+ httpPost.setEntity(s);
|
|
|
+ System.out.println("请求地址:" + url);
|
|
|
+ System.out.println(json);
|
|
|
+ httpPost.setHeader("Content-type", "application/json");
|
|
|
+ httpPost.setHeader("-UserAgent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
+
|
|
|
+
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+ System.out.println(response);
|
|
|
+
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ System.out.println(entity);
|
|
|
+ if (entity != null) {
|
|
|
+
|
|
|
+ body = EntityUtils.toString(entity, encoding);
|
|
|
+ }
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+
|
|
|
+ response.close();
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getJsonData(JSONObject jsonParam,String urls) {
|
|
|
+ StringBuffer sb=new StringBuffer();
|
|
|
+ try {
|
|
|
+
|
|
|
+ URL url = new URL(urls);
|
|
|
+
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ conn.setDoOutput(true);
|
|
|
+
|
|
|
+ conn.setDoInput(true);
|
|
|
+
|
|
|
+ conn.setUseCaches(false);
|
|
|
+
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+
|
|
|
+ conn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+
|
|
|
+ conn.setRequestProperty("Charset", "UTF-8");
|
|
|
+
|
|
|
+ byte[] data = (jsonParam.toString()).getBytes();
|
|
|
+
|
|
|
+ conn.setRequestProperty("Content-Length", String.valueOf(data.length));
|
|
|
+
|
|
|
+ conn.setRequestProperty("contentType", "application/json");
|
|
|
+
|
|
|
+ conn.connect();
|
|
|
+ OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
|
|
|
+
|
|
|
+ out.write((jsonParam.toString()).getBytes());
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ System.out.println(conn.getResponseCode());
|
|
|
+
|
|
|
+
|
|
|
+ if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
|
|
|
+ System.out.println("连接成功");
|
|
|
+
|
|
|
+ InputStream in1 = conn.getInputStream();
|
|
|
+ try {
|
|
|
+ String readLine = new String();
|
|
|
+ BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
|
|
|
+ while((readLine=responseReader.readLine())!=null){
|
|
|
+ sb.append(readLine).append("\n");
|
|
|
+ }
|
|
|
+ responseReader.close();
|
|
|
+ System.out.println(sb.toString());
|
|
|
+
|
|
|
+ } catch (Exception e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("error++");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|