Commit c743315f authored by dumuyang's avatar dumuyang

工具类提交

parent 2b9294b0
......@@ -25,6 +25,10 @@
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
......
......@@ -5,11 +5,21 @@ import com.yanzuoguang.util.exception.HttpCodeException;
import com.yanzuoguang.util.thread.ProcessData;
import com.yanzuoguang.util.thread.RunProcess;
import com.yanzuoguang.util.vo.MapRow;
import org.apache.commons.codec.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
......@@ -414,5 +424,38 @@ public class HttpHelper {
}
}
/**
* post请求form-data
* @param url
* @param parametersBody
* @return
* @throws Exception
*/
public static String postRequest(String url, List<NameValuePair> parametersBody) throws Exception {
HttpEntity entity = new UrlEncodedFormEntity(parametersBody, Charsets.UTF_8);
return post(url, "application/x-www-form-urlencoded", entity);
}
private static String post(String url, String mediaType, HttpEntity entity) throws Exception {
HttpPost post = new HttpPost(url);
post.addHeader("Content-Type", mediaType);
post.addHeader("Accept", "application/json");
post.setEntity(entity);
String var8;
try {
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code >= 400) {
throw new Exception(EntityUtils.toString(response.getEntity()));
}
var8 = EntityUtils.toString(response.getEntity());
} catch (Exception var11) {
throw new Exception("postRequest -- Client protocol exception!", var11);
} finally {
post.releaseConnection();
}
return var8;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment