import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
String url ="请求地址";
String token ="";
String body ="请求参数";
HttpPost post = null;
String responseMessange = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
post = new HttpPost(url);
// 构造消息头
post.setHeader("Content-type", "application/x-www.form-urlencoded; charset=utf-8");
if (!"".equals(token))
post.setHeader("x-wlk-Authorization", token);
// 构建消息实体
StringEntity entity = new StringEntity(body, Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
// 发送Json格式的数据请求
entity.setContentType("application/x-www.form-urlencoded");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
try {
// 获取响应实体
HttpEntity httpEntity = response.getEntity();
if (entity != null) {
responseMessange = EntityUtils.toString(httpEntity);
System.out.println("响应"+responseMessange);
JSONObject parseObject = JSONObject.parseObject(responseMessange);
JSONArray array = parseObject.getJSONArray ("Data");
JSONObject o = (JSONObject) array.get(1);
}
} finally {
((Closeable) response).close();
}
} catch (Exception e) {
LOG.error("post请求失败", e);
System.out.println("post请求失败"+ e);
} finally {
// if (post != null) {
// try {
post.releaseConnection();
// Thread.sleep(500);
// } catch (InterruptedException e) {
// LOG.error("关闭连接失败", e);
// System.out.println("关闭连接失败"+e);
// }
// }
}
发送Hppt请求
于 2022-08-18 17:51:10 首次发布