package com.ruoyi.other.util;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpStatus;
|
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
import java.io.IOException;
|
|
public class HttpUtils {
|
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
|
public static HttpResponse doPost(String url, JSONObject jsonData) {
|
// 指定Post请求
|
HttpPost httpPost = new HttpPost(url);
|
// 创建httpclient
|
HttpClient httpClient = new DefaultHttpClient();
|
// 发送请求
|
HttpResponse httpResponse;
|
// 返回的json
|
JSONObject jsonObject = null;
|
// 封装post请求数据
|
StringEntity entity = new StringEntity(jsonData.toString(), "utf-8");
|
httpPost.setEntity(entity);
|
try {
|
// 发送请求
|
httpResponse = httpClient.execute(httpPost);
|
// 判断请求是否成功
|
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
// 得到请求响应信息
|
// String str = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
|
// 返回json
|
// jsonObject = new JSONObject(Boolean.parseBoolean(str));
|
return httpResponse;
|
}
|
} catch (ClientProtocolException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|