1、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)乱码的问题,这个问题费了我好大的劲才解决。。。
2、java原生HttpClient3.1 post请求
/**
* java原生HttpClient3.1 post请求
*/
@SuppressWarnings("unchecked")
public static Map sendPost2(String sendMsg, String sendUrl) {
HttpClient httpclient = null;
PostMethod post = null;
String info = "";
Map mres = new HashMap();
try {
httpclient = new HttpClient();
post = new PostMethod(sendUrl);
//解决中文乱码问题
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
post.addParameter("info", sendMsg);
httpclient.executeMethod(post);
info = new String(post.getResponseBody(), "UTF-8");
mres = (Map) JSONObject.toBean(JSONObject.fromObject(info), Map.class);
} catch (Exception e) {
mres.put("status", "9999");
mres.put("message", "请求处理异常" + e.getMessage());
} finally {
// 关闭连接,释放资源
post.releaseConnection();
}
return mres;
}
接收请求:
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
注意:produces="application/json;charset=UTF-8" 解决返回结果集中文乱码问题。