java接收http请求示例_JAVA httpClient4.5 post请求及接收示例代码

import java.util.HashMap;

import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.config.RequestConfig;

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.HttpClientBuilder;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

public class SendHttp {

/**

* java httpClient4.5 post请求

*/

@SuppressWarnings("unchecked")

public static Map sendPost(String sendMsg, String sendUrl) {

HttpPost httpPost = new HttpPost(sendUrl);

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

StringEntity entity;

Map mres = new HashMap();

try {

entity = new StringEntity(sendMsg, "UTF-8"); //解决参数中文乱码问题

entity.setContentEncoding("UTF-8");//设置编码格式

entity.setContentType("application/json");

httpPost.setEntity(entity);

// 发起请求

HttpResponse httpResponse = closeableHttpClient.execute(httpPost);

// 请求结束,返回结果。并解析json。

String resData = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");

mres = (Map) JSONObject.toBean(JSONObject.fromObject(resData), Map.class);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null != closeableHttpClient) {

try {

closeableHttpClient.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

return mres;

}

//测试

public static void main(String[] args) throws Exception {

Map sendMsg = new HashMap();

sendMsg.put("user_name", "lanqinger");

sendMsg.put("real_name", "蓝卿儿");

String sendUrl = "http://localhost:8080/sm/test/getPostData.form";

Map result = sendPost(JSONObject.fromObject(sendMsg).toString(),sendUrl);

System.out.println(result);

}

}

注意:StringEntity entity = new StringEntity(sendMsg, "UTF-8");才是真正解决传参(@RequestBody String requestBody)乱码的问题,这个问题费了我好大的劲才解决

import java.util.HashMap;  

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.lhm.service.UserInfoService;

import com.lhm.value.UserInfoPage;

@Controller

@RequestMapping("/test")

public class TestController {

@Resource

private UserInfoService userInfoService;

@RequestMapping(value="/getPostData.form", produces="application/json;charset=UTF-8")

@ResponseBody

public Object getPostData(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {

JSONObject json = JSONObject.fromObject(requestBody);

UserInfoPage userInfoPage = (UserInfoPage) JSONObject.toBean(json, UserInfoPage.class);

userInfoPage.setCurrentPage(1);

userInfoPage.setPageSize(10);

List> list = userInfoService.listUsersMap(userInfoPage);

Map map = new HashMap();

map.put("success", "1");

map.put("total", list.size());

map.put("message", "执行成功");

return JSONObject.fromObject(map).toString();

}

}注意:produces="application/json;charset=UTF-8" 解决返回结果集中文乱码问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值