文档地址:
请求参数:
{
"shipping_list": [
{
"item_desc": "书籍"
}
],
"upload_time": "2023-10-23T16:21:27.144+08:00",
"order_key": {
"transaction_id": "4200001985202310235326041813",
"order_number_type": 2
},
"logistics_type": 4,
"payer": {
"openid": "oMZf_5DPXWNH0cHxIKcEISo56"
},
"delivery_mode": 1
}
发起POST请求:
HttpPost httpPost = new HttpPost(httpUrl);
//以json形式提交请求
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
返回状态正常,已经发货成功,但是在微信客户端服务通知中商品信息乱码,大佬么是怎么解决的?
示例代码:
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.apache.http.HttpEntity; 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.util.EntityUtils; public class Main { public static void main(String[] args) { String httpUrl = "http://example.com/api"; String json = "{\"shipping_list\": [{\"item_desc\": \"书籍\"}],\"upload_time\": \"2023-10-23T16:21:27.144+08:00\",\"order_key\": {\"transaction_id\": \"4200001985202310235326041813\",\"order_number_type\": 2},\"logistics_type\": 4,\"payer\": {\"openid\": \"oMZf_5DPXWNH0cHxIKcEISo56\"},\"delivery_mode\": 1}"; try { json = URLEncoder.encode(json, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } HttpPost httpPost = new HttpPost(httpUrl); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); httpPost.setEntity(new StringEntity(json, "UTF-8")); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); System.out.println(result); } } catch (Exception e) { e.printStackTrace(); } } }
//设置传输超时时间
.setSocketTimeout(timeout <= 0 ? 30000 : timeout)
.build();
httpPost.setConfig(config);
//执行请求
response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity!=null){
String s = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
result = JSONObject.parseObject(s);
}else {
//如果entity为空,那么直接消化掉即可
EntityUtils.consume(httpEntity);
}
}
} catch (IOException e) {
log.error("发送post请求错误,请求URL:{},请求参数:{},错误信息:{}", httpUrl, data, e.getMessage());
} finally {
//释放链接
releaseConnection(client, response);
}
return result;
}
这个是post代码,请大佬指导下,感谢感谢!
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
JSONObject result = null;
try {
//创建可关闭的 HttpClient 实例
client = HttpClients.createDefault();
//创建 HttpPost 实例并设置请求头
HttpPost httpPost = new HttpPost(httpUrl);
//以json形式提交请求
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
if (Objects.nonNull(header) && header.size() > 0) {
Set<Map.Entry<String, String>> entries = header.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
//设置请求参数和参数格式
StringEntity entity = new StringEntity(JSONObject.toJSONString(data));
entity.setContentType("application/json;charset=UTF-8");
httpPost.setEntity(entity);
//请求配置项
RequestConfig config = RequestConfig.custom()
//设置连接超时时间
.setConnectTimeout(timeout <= 0 ? 30000 : timeout)
//设置传输超时时间
.setSocketTimeout(timeout <= 0 ? 30000 : timeout)
.build();
httpPost.setConfig(config);
//执行请求
response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity!=null){
String s = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
result = JSONObject.parseObject(s);
}else {
//如果entity为空,那么直接消化掉即可
EntityUtils.consume(httpEntity);
}
}
} catch (IOException e) {
log.error("发送post请求错误,请求URL:{},请求参数:{},错误信息:{}", httpUrl, data, e.getMessage());
} finally {
//释放链接
releaseConnection(client, response);
}
return result;
}
json化的时候转义了,用这个
$str = json_encode($a, JSON_UNESCAPED_UNICODE);