Java发送https请求 post请求,请求报文形式区别

存在问题:请求时接口返回参数缺失,或者格式错误,反正就是各种明明正确的错
解决方案:正常浏览器地址栏请求的都是get请求,正常post请求的都是以表单的形式,但是也有以json的格式传输,虽说在接口文档里会说明,但是容易被忽略,在此将三种请求方式记录下。


第一种:get请求,复制直接能用
protected String get(String url){
String result = null;
HttpClient httpClient = null;
try {
httpClient = new SSLClient();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpGet.setHeader("Accept-Charset", "utf-8");
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity,"GBK");
EntityUtils.consume(httpEntity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
下图为以上代码:(URL例子:http://localhost:8080//face/nt/smallprogramcontroler/testPay.jhtml?a=1&b=2)![在这里插入图片描述](https://img-blog.csdnimg.cn/20181108203421751.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTg5NTgxNw==,size_16,color_FFFFFF,t_70)


第二种请求:post表单格式请求(和第三种的主要区别标明了)
protected String postbd(String url, Map<String, String> parameterMap) {
//System.out.println("87提速开始+++++++++++++"+CommonUtils.getDateStr());
Assert.hasText(url);
String result = null;
HttpClient httpClient = null;
try {
httpClient = new SSLClient();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpParams params = httpClient.getParams();
//设置请求超时5秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * 1000);
//设置等待数据超时时间10秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
//在提交请求之前 测试连接是否可用
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
try {
// System.out.println("post====="+JsonUtils.toJson(parameterMap));
HttpPost httpPost = new HttpPost(url);
// httpPost.setEntity(new StringEntity(JsonUtils.toJson(parameterMap),"UTF-8"));

**

List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
for(String key:parameterMap.keySet()){
list.add(new BasicNameValuePair(key,parameterMap.get(key)));
}

**
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(list,"utf-8");
httpPost.setEntity(uefe);
System.out.println("POST请求 JsonUtils.toJson(parameterMap)"+list.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
} catch (Exception e) {
// System.out.println("87提速异常+++++++++++++"+CommonUtils.getDateStr());
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
// System.out.println("result======="+result);
return result;
}
下图为需要传入的参数map:(地址就是正常的请求不带参数地址)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181108203808611.png)

第三种 用的比较少不多见 json格式请求(请求map同上 两个的区别加粗了)
protected String post(String url, Map<String, String> parameterMap) {
//System.out.println("87提速开始+++++++++++++"+CommonUtils.getDateStr());
Assert.hasText(url);
String result = null;
HttpClient httpClient = null;
try {
httpClient = new SSLClient();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//==================================================
HttpParams params = httpClient.getParams();
//设置请求超时5秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * 1000);
//设置等待数据超时时间10秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
//在提交请求之前 测试连接是否可用
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
//==================================================

try {
// System.out.println("post====="+JsonUtils.toJson(parameterMap));
HttpPost httpPost = new HttpPost(url);
**`httpPost.setEntity(new StringEntity(JsonUtils.toJson(parameterMap),"UTF-8"));*`*
System.out.println("POST请求 JsonUtils.toJson(parameterMap)"+JsonUtils.toJson(parameterMap));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
} catch (Exception e) {
// System.out.println("87提速异常+++++++++++++"+CommonUtils.getDateStr());
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
// System.out.println("result======="+result);
return result;
}
记录一下,以后回头看。

posted @ 2021-02-20 13:49  偏丿执  阅读(132)  评论(0编辑  收藏  举报