http请求的参数可以放到headers里面,也可以放到body里面,可以get请求也可以post请求
1.post请求方式:
1.1 传参:参数放到headers里面(postman工具可以测试所有的场景)
public static String sendPost(String url, Map headers, String data) {
String response = null;
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
//数据放到body里面
StringEntity stringentity = new StringEntity(data, ContentType.create("application/json", "UTF-8"));
httppost.setEntity(stringentity);
// 循环添加header 数据放到header里面
Iterator headerIterator = headers.entrySet().iterator();
while (headerIterator.hasNext()) {
Entry elem = (Entry) headerIterator.next();
httppost.addHeader((String) elem.getKey(), (String) elem.getValue());
}
//发post请求
httpresponse = httpclient.execute(httppost);
//utf-8参数防止中文乱码
response = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;}
1.2 获取headers里面的方法
public static Map<String, String> getHeadersInfo(HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
1.3 获取body里面数据的方法(用流的方式读取)
public static String getHttpBody(HttpServletRequest request) {
String data = "";
try {
InputStream in = request.getInputStream();
InputStreamReader isr = new InputStreamReader(in, "utf-8");
BufferedReader br = new BufferedReader(isr);
for(String temp = ""; (temp = br.readLine()) != null; data = data + temp) {
;
}
} catch (IOException var7) {
var7.printStackTrace();
}
if (data.indexOf("BODY_DATA") != -1) {
try {
data = URLDecoder.decode(data.substring("BODY_DATA=".length()), "utf-8");
} catch (UnsupportedEncodingException var6) {
var6.printStackTrace();
}
}
return data;}
2.参数放到body里面
2.1
public static String invokeHttpPost(String requestUrl, String param) throws Exception {
logger.info("调用远程接口参数:{}", param);
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(requestUrl);
StringRequestEntity entity = null;
String response = "";
try {
entity = new StringRequestEntity(param, "application/json; charset=utf-8", "UTF-8");
postMethod.setRequestEntity(entity);
int statusCode = client.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
response = new String(postMethod.getResponseBody(), "utf-8");
logger.info("远程调用接口:{},返回数据:{}", requestUrl, response);
} else {
logger.info("调用远程接口异常 httpStatus:{}", statusCode);
throw new Exception("调用远程接口异常 httpStatus:" + statusCode);
}
postMethod.releaseConnection();
logger.info(response);
return response;
} catch (Exception e) {
logger.info("请求远程接口异常:{}", requestUrl, e);
throw new Exception("请求接口异常");
}
}
2.2
获取body里面数据的方法(用流的方式读取)
public static String getHttpBody(HttpServletRequest request) {
String data = "";
try {
InputStream in = request.getInputStream();
InputStreamReader isr = new InputStreamReader(in, "utf-8");
BufferedReader br = new BufferedReader(isr);
for(String temp = ""; (temp = br.readLine()) != null; data = data + temp) {
;
}
} catch (IOException var7) {
var7.printStackTrace();
}
if (data.indexOf("BODY_DATA") != -1) {
try {
data = URLDecoder.decode(data.substring("BODY_DATA=".length()), "utf-8");
} catch (UnsupportedEncodingException var6) {
var6.printStackTrace();
}
}
return data;}
get请求
1.正常的get请求获取?后面参数的方法:
//第一种
String ss = request.getQueryString(); 结果:username=password
//第二种
Enumeration<string> paraNames=request.getParameterNames();</string>
for(Enumeration<String> e=paraNames;e.hasMoreElements();){
String thisName=e.nextElement()+""; //name名
String thisValue=request.getParameter(thisName); //name名对应的值
System.out.println(thisName+" : "+thisValue);
}request.getInputStream() / request.getReader()只能获取POST请求的参数,即请求体的参数。且请求体中的数据仅能调用 request.getInputStream(), request.getReader()和request.getParameter("key") 方法读取一次,即仅第一次能读到数据,后面读到的数据为null。
详细参见其他人博客:https://blog.csdn.net/xybz1993/article/details/80655991



京公网安备 11010502036488号