package com.example.climbnumber.yzm; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; 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.util.EntityUtils; public class Test04 { public static void main(String[] args) { JSONObject jsonObject = JSONObject.parSEObject("{\n" + " \"body\": {\n" + " \"uuid\": \"\",\n" + " \"scenes\": \"xxxx(91522301MA6DJJYG2C)\"\n" + " },\n" + " \"head\": {\n" + " \"transCode\": \"\",\n" + " \"traceId\": \"\"\n" + " }\n" + "}"); JSONObject result = sendPost("www.baidu.com",jsonObject); System.out.println("================================"); System.out.println(result.toJSONString()); } public static JSONObject sendPost(String url,JSONObject param){ //定义接收数据 JSONObject result = new JSONObject(); HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); //请求参数转JOSN字符串 StringEntity entity = new StringEntity(param.toJSONString(), "UTF-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); try { HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { result = JSON.parSEObject(EntityUtils.toString(response.getEntity(), "UTF-8")); } } catch (Exception e) { e.printstacktrace(); result.put("error", "连接错误!"); } return result; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。