传数据给第三方接口,乱码问题

传数据给第三方接口,

设置请求类型,请求头,请求体

重要:!!防止乱码!!



	public RespEntity createCustomers(String email,String name) throws Exception {

		
		// 超级管理员邮箱
		String adminemail = "[email protected]";
		// 鉴权token
		String token = "xxxxx-5xxx-4exxxx-xxxxxxxxxx9b9";
		System.out.println(token.length());
		// 10位时间戳
		long timestamp10 = System.currentTimeMillis() / 1000L;
		// 签名算法
		@SuppressWarnings("deprecation")
		String sign = DigestUtils.shaHex(adminemail + "&" + token + "&" + timestamp10);
		// 创建客户  url
		String url = "http://xxxx-xx-xxx.xx.cn/xxxx/customers?email="
				+ adminemail + "×tamp=" + timestamp10 + "&sign=" + sign;
		log.info("创建客户 url= {}",url);


		if(StringUtils.isBlank(url)) {
			return RespEntity.error(RespCode.COMMON_PARAM_BLANK);
		}
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;
		String content = null;
		

		String customer="{\n" + 
				"    \"customer\": {\n" + 
				"        \"email\":\""+email+"\",\n" + 
				"        \"nick_name\": \""+name+"\",\n" + 
				"        \"custom_fields\": {\n" + 
				"            \"TextField_25775\": \"普通文本内容\"\n" + 
				"        },\n" + 
				"        \"weixins\": [\n" + 
				"        ]\n" + 
				"    },\n" + 
				"    \"other_emails\": [\n" + 
				"    ]\n" + 
				"}";
		

		httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
		StringEntity stringEntity = new StringEntity(customer,Charset.forName("UTF-8"));
		stringEntity.setContentEncoding("UTF-8");
		httpPost.setEntity(stringEntity);
		
		try {
			// 执行请求
			response = httpclient.execute(httpPost);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				content = EntityUtils.toString(response.getEntity(), "UTF-8");
			} else {
				content = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
			System.out.println(content);
		} finally {
			if (response != null) {
				response.close();
			}
			httpclient.close();
		}
		
		JSONObject json = JSONObject.parseObject(content);
		log.info("请求结束,第三方返回的数据为  json={}",json);
		return  RespEntity.success(json);
	}


你可能感兴趣的:(io)