|
|
@@ -1,99 +0,0 @@
|
|
|
-package com.uas.platform.b2bManage.core.util;
|
|
|
-
|
|
|
-import org.apache.http.HttpResponse;
|
|
|
-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.protocol.HTTP;
|
|
|
-
|
|
|
-import java.io.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * created by shicr on 2017/11/24
|
|
|
- **/
|
|
|
-public class HttpUtil {
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送post请求
|
|
|
- *
|
|
|
- * @param postUrl
|
|
|
- * @param formData
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static Response doPost(String postUrl, String formData, int timeout) throws Exception {
|
|
|
- CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
- HttpPost post = new HttpPost(postUrl);
|
|
|
- StringEntity postingString = new StringEntity(formData, HTTP.UTF_8);
|
|
|
- post.setEntity(postingString);
|
|
|
- post.setHeader("Content-type", "application/json");
|
|
|
- CloseableHttpResponse response = httpClient.execute(post);
|
|
|
- return Response.getResponse(response);
|
|
|
- }
|
|
|
- public static class Response {
|
|
|
- private int statusCode;
|
|
|
- private String responseText;
|
|
|
-
|
|
|
- public int getStatusCode() {
|
|
|
- return statusCode;
|
|
|
- }
|
|
|
-
|
|
|
- public void setStatusCode(int statusCode) {
|
|
|
- this.statusCode = statusCode;
|
|
|
- }
|
|
|
-
|
|
|
- public String getResponseText() {
|
|
|
- return responseText;
|
|
|
- }
|
|
|
-
|
|
|
- public void setResponseText(String responseText) {
|
|
|
- this.responseText = responseText;
|
|
|
- }
|
|
|
-
|
|
|
- public Response() {
|
|
|
- }
|
|
|
-
|
|
|
- public Response(boolean success, String content) {
|
|
|
- super();
|
|
|
- this.statusCode = success ? 200 : 404;
|
|
|
- this.responseText = content;
|
|
|
- }
|
|
|
-
|
|
|
- public Response(HttpResponse response) throws IllegalStateException, IOException, Exception {
|
|
|
- this.statusCode = response.getStatusLine().getStatusCode();
|
|
|
- this.responseText = HttpUtil.read2String(response.getEntity().getContent());
|
|
|
- }
|
|
|
-
|
|
|
- public static Response getResponse(HttpResponse response) throws IllegalStateException, IOException, Exception {
|
|
|
- if (response != null)
|
|
|
- return new Response(response);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将输入流转为字符串
|
|
|
- *
|
|
|
- * @param inStream
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static String read2String(InputStream inStream) throws Exception {
|
|
|
- ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int len = 0;
|
|
|
- while ((len = inStream.read(buffer)) != -1) {
|
|
|
- outSteam.write(buffer, 0, len);
|
|
|
- }
|
|
|
- try {
|
|
|
- outSteam.close();
|
|
|
- inStream.close();
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
- return new String(outSteam.toByteArray(), "UTF-8");
|
|
|
- }
|
|
|
-
|
|
|
-}
|