需要的包
1.commons-httpclient-3.1.jar
2.dom4j-1.6.1.jar
post方式
public void sendPost(String url,String parms){
InputStream inputStream = null;
try{
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(requestUrl);
// 设置请求头 Content-Type
postMethod.setRequestHeader("Content-Type", "application/xml");
//针对于Bearer token
postMethod.setRequestHeader("Authorization", "Bearer " +"eyJjbGllbnQtaWQiOiJTUEQiLCJhbGciOiJSUzI1NiIsImtpZCI6IjUxRDVGM0EyLUYwRkItNEY4OC04NTNGLTlDRjMxRjAwNkYyMiJ9." + "eyJzdWIiOiJhZG1pbiIsImF1ZCI6IlNQRCIsIm5iZiI6MTY3NTMwNzI1Nywib3JnQ29kZSI6IjEyNDUwMzI4TUIwMDIzMjUzRSIsInNjb3BlIjpbIlwvKioiXSwiaXNzIjoiaHR0cDpcL1wvMTkyLjE2OC4yMDAuOTo5MDkwXC9oaXMiLCJleHAiOjIzMDYwMjcyNjMsImlhdCI6MTY3NTMwNzI1N30." + "FCgjRBbJD1DaSEMVg3oevBZhDGrPDY_ZTiJR-AMOBUyJs8A0mo4xoKYE-LHmVpMNXqUtngb4wWT9X74ZQ2fCXlTA0qOus6XfgMyPjGUsm-UuSyCnQqracXNlj6xUdcABaDXK7D6H7GPRNNmbqOkTscBzZHjEWfWQuUpPRJtyEQyFMAuKQrJbj_DcdiB3VpgcNH3wt3qe7q6LFkbr17jbOcBB6ZkPwJI" +"sUdpP2xppDYunpixv_XGzm-FIiE1lROxmqe9_fiHPIhkcOAYOLaA6LmuzMnQWzFxPBuVP3PC-isuApHUYkA8YCnjCqtCr_Jnp33QpLarwCN-CFjlHFHwgxQ");
//这里用了xml格式的参数
Document doc = DocumentHelper.parseText(params);
String xml = doc.asXML();
RequestEntity requestEntity = new StringRequestEntity(xml,"application/xml","UTF-8");
postMethod.setRequestEntity(requestEntity);
// 执行请求
httpClient.executeMethod(postMethod);
inputStream = postMethod.getResponseBodyAsStream();// 获取返回的流
}catch(Exception e){
log.error("");
}finally{
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}