httpclient的代码
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
JSONObject j=new JSONObject();
j.put("client_id", "XXXX");
j.put("client_secret", "XXXXXXX");
StringEntity entity = new StringEntity(j.toString().trim(),"utf-8");//解决中文乱码问题
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
HttpEntity responseEntity=null;
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
int status= response.getStatusLine().getStatusCode();
System.out.println(EntityUtils.toString(response.getEntity()));
if(status==302) {
}
这个能得到正确的status302
JSONObject j=new JSONObject();
j.put("client_secret","xxxx");
j.put("client_id","xxxxx");
Request req = Request.create(url, METHOD.POST);
req.setData(j.toString().trim());
org.nutz.http.Header header =req.getHeader();
header.set("Content-Type", "application/json;charset=utf-8");
System.out.println("header="+req.getHeader().get("Content-Type"));
Sender sender=Sender.create(req);
Response response=sender.send();
System.out.println(response.getStatus());
这个不能的到正确status302
他们差别在哪儿啊