依赖org.apache.httpcomponentshttpclient4.5.6简单GET请求publicstaticStringGet(Stringurl)throwsIOException{//创建对象CloseableHttpClienthttpClient=HttpClientBuilder.create().build();//GET请求路径HttpGethttpGet=newHttpGet(url);httpGet.setHeader("Content-Type","application/json;charset=utf8");//响应CloseableHttpResponseresponse=httpClient.execute(httpGet);//获取状态码intstatusCode=response.getStatusLine().getStatusCode();//只能获取HttpEntityonceentity=response.getEntity();字符串结果=EntityUtils.toString(实体);返回结果;}简单的POST请求publicstaticStringPost(Stringurl,Objectparam)throwsIOException{//CreateobjectCloseableHttpClienthttpClient=HttpClientBuilder.create().build();//POST请求路径HttpPosthttpPost=newHttpPost(url);httpPost.setHeader("Content-Type","application/json;charset=utf8");//放入请求EntityJSONObjectjsonObject=newJSONObject();jsonObject.put("大小","1");jsonObject.put("当前","1");StringEntityentity=newStringEntity(jsonObject.toString(),"UTF-8");httpPost.setEntity(entity);//ResponseCloseableHttpResponseresponse=httpClient.execute(httpPost);//获取对象(response.getEntity()只能获取一次)Stringresult=EntityUtils.toString(response.getEntity());返回结果;}POST请求/****@paramurl请求路径*@paramheader请求头*@paramparam对象参数*@param路径拼接参数*@param类型拼接pathtrue:PathVariable拼接,false:RequestParam拼接,""不是拼接*@return*/publicstaticStringisPost(Stringurl,Mapheader,Objectparam,Mappath,Stringtype){//创建CloseableHttpClienthttpClient=HttpClientBuilder.create().build();CloseableHttpResponse响应=空;字符串uri="";try{//是否需要路径拼接if(!type.isEmpty()){if(type.equals("true")){//PathVariable拼接if(!path.isEmpty()){for(Stringkey:path.keySet()){uri+="/"+path.get(key);}//System.out.println("拼接路径:"+uri);}}elseif(type.equals("false")){//RequestParam拼接if(!path.isEmpty()){uri="?";对于(字符串键:path.keySet()){uri+=key+"="+path.get(key)+"&";}uri=uri.substring(0,uri.length()-1);}}}url=url+uri;System.out.println("访问路径:"+url);//HttpPostHttpPosthttpPost=newHttpPost(url);//判断是否为空if(!header.isEmpty()){for(Stringkey:header.keySet()){//存储在头部httpPost.addHeader(key,header.get(key));}}httpPost.setHeader("Content-Type","application/json;charset=utf8");//转成JSON字符串StringjsonString=JSON.toJSONString(param);StringEntityentity=newStringEntity(jsonString,"UTF-8");//入参httpPost.setEntity(entity);//获取响应体response=httpClient.execute(httpPost);//获取响应体的状态//System.out.println(response.getStatusLine().getStatusCode());//HttpClient只允许一个getEntityStringresult=EntityUtils.toString(response.getEntity());System.out.println("响应状态:"+response.getStatusLine().getStatusCode());System.out.println("响应数据:"+result);返回结果;}catch(ClientProtocolExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}最后{尝试{httpClient.close();}catch(IOExceptione){e.printStackTrace();}试试{response.close();}catch(IOExceptione){e.printStackTrace();}}返回“执行错误!”;}GET/***httpClient获取请求*@paramurl请求url*@paramparam请求参数适用于表单提交*@return可能为空,需要处理*@throwsException**/publicstaticStringsendHttpsGet(Stringurl,Stringparam,Stringsign)throwsException{Stringresult="";CloseableHttpClienthttpClient=null;尝试{httpClient=getHttpClient();JSONObject解析=(JSONObject)JSONObject.parse(param);整数pageCount=(Integer)parse.get("pageCount");整数pageNo=(Integer)parse.get("pageNo");如果(pageCount==pageNo){}else{url+="?pageCount="+pageCount+"/pageNo="+pageNo;}log.info("请求地址:"+url);HttpGethttpGet=newHttpGet(url);/***设置请求头*/httpGet.addHeader("Content-Type","application/json");httpGet.addHeader("授权",sign);//httpPost.addHeader("密码","3er4#ER$3er4#ER$12");/***设置请求参数*///System.out.println("参数"+param);//StringEntitystringEntity=newStringEntity(param,"utf-8");//httpGet.setEntity(stringEntity);HttpResponsehttpResponse=httpClient.execute(httpGet);intstatusCode=httpResponse.getStatusLine().getStatusCode();if(statusCode==HttpStatus.SC_OK){HttpEntityresEntity=httpResponse.getEntity();结果=EntityUtils.toString(resEntity);}else{结果=readHttpResponse(httpResponse);}}catch(Exceptione){log.info("sendlookalikehttpgetrequestfailed,HttpException"+e);扔e;}最后{}返回结果;}