今天就跟大家聊聊有關(guān)http請(qǐng)求怎么在spring boot中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站與策劃設(shè)計(jì),福清網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:福清等地區(qū)。福清做網(wǎng)站價(jià)格咨詢:13518219792
首先是經(jīng)過封裝:
一:初始化httpclient
private static HttpClient client = null; static { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(128); cm.setDefaultMaxPerRoute(128); client = HttpClients.custom().setConnectionManager(cm).build(); }
二:獲取請(qǐng)求的url,因?yàn)槲曳?wù)端定義的url與客戶端一樣,所以我直接使用請(qǐng)求客戶端的url
//根據(jù)request獲取請(qǐng)求的url public StringBuffer getUrlToRequest(HttpServletRequest request) { StringBuffer url=request.getRequestURL();//獲取請(qǐng)求的url(http://localhost:8080/switch/getStudentInfoByName) String[] splitArr=url.toString().split("/"); String appName=splitArr[3];//項(xiàng)目名稱 String ipReport=splitArr[2];//項(xiàng)目ip:report String resultStr=url.toString().replaceAll(appName,DevFacConstans.facname).replaceAll(ipReport, DevFacConstans.ip+":"+DevFacConstans.report); return new StringBuffer(resultStr); }
獲取url根據(jù)/ 進(jìn)行split,因?yàn)槲疫@是測(cè)試環(huán)境,生產(chǎn)環(huán)境ip,端口號(hào)(域名)肯定不是localhost,有的前面還會(huì)加上項(xiàng)目名稱,所以我split對(duì)應(yīng)的值來進(jìn)行替換。
三:拼裝請(qǐng)求參數(shù),調(diào)用http請(qǐng)求
/** * 發(fā)送http請(qǐng)求 有request * 給controller層調(diào)用 * @param request * @return */ public String sendHttpToDevFac(HttpServletRequest request)throws Exception { HttpClient client = null; String returnResult=""; // http://localhost:8080/leo/1.0/h6/login StringBuffer urlBuffer=getUrlToRequest(request);//調(diào)用第二步,獲取url //獲取參數(shù)并拼裝 String dataAsJson = request.getParameter("data"); String encoderData=URLEncoder.encode(dataAsJson,"utf-8"); HttpGet get=new HttpGet(urlBuffer.append("?data=").append(encoderData).toString()); //set headers EnumerationheaderNames=request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName=headerNames.nextElement(); String headerValue=request.getHeader(headerName); get.setHeader(headerName, headerValue); } client=DeviceFactoryHttp.client; logger.info("開始調(diào)用http請(qǐng)求,請(qǐng)求url:"+urlBuffer.toString()); HttpResponse rep=client.execute(get); returnResult=EntityUtils.toString(rep.getEntity(),"utf-8"); logger.info("http 請(qǐng)求調(diào)用結(jié)束!!"); return returnResult; }
先獲取請(qǐng)求的參數(shù),再將參數(shù)拼裝在url后面,URLEncoder.encode 這個(gè)不要忘了,因?yàn)閰?shù)會(huì)有一些符號(hào),需要對(duì)參數(shù)進(jìn)行編碼后再加入url,否則就會(huì)拋出異常,set headers:因?yàn)橛胁糠中畔⒎?wù)端會(huì)從請(qǐng)求頭中取出,所以我將客戶端的請(qǐng)求頭也set到服務(wù)端的request中,請(qǐng)求的url和請(qǐng)求的參數(shù)拼好就就可以client.exceute(get)執(zhí)行請(qǐng)求了。
上面的是我瀏覽器直接將request請(qǐng)求作為參數(shù)傳到我客戶端,我所以我可以直接從request中獲取url,有的是沒有request,就需要從request的上下文環(huán)境中取了。
沒有經(jīng)過封裝的:
首先從上下文中獲取request的
/** * 獲取request * @return */ public static HttpServletRequest getRequest(){ ServletRequestAttributes ra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ra.getRequest(); return request; }
二:有了request后,就有了url,下面再來解析請(qǐng)求參數(shù),因?yàn)檫@個(gè)參數(shù)是沒有封裝的,所以獲取所有的請(qǐng)求參數(shù)
/** * 沒有request 請(qǐng)求,給controller層調(diào)用 * @param key * @param interfaceName * @param strings * @return * @throws Exception */ public String centerToDeviceFacNoRequest(String key,String interfaceName)throws Exception { try { HttpServletRequest request=getRequest();//上面第一步,從上下文中獲取url //獲取reuquest請(qǐng)求參數(shù) Enumerationnames= request.getParameterNames(); Map paramMap=new HashMap<>(); //遍歷請(qǐng)求map while(names.hasMoreElements()) { String name=names.nextElement(); String value=(String) request.getParameter(name); paramMap.put(name, value); } //調(diào)用發(fā)送http請(qǐng)求的方法 return sendHttpToDevFacNoData(paramMap,request); } catch (Exception e) { e.printStackTrace(); } //end return null; }
三:發(fā)送http請(qǐng)求
/** * 發(fā)送http請(qǐng)求,【沒有data數(shù)據(jù)的】 * @return */ public String sendHttpToDevFacNoData(MapparamMap,HttpServletRequest request)throws Exception { HttpClient client = null; String result=""; StringBuffer dataBuffer=getUrlToRequest(request);//獲取url dataBuffer.append("?"); client=DeviceFactoryHttp.client; Iterator > paamIt=paramMap.entrySet().iterator(); while(paamIt.hasNext()) { Entry entry=paamIt.next(); dataBuffer.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } String resultUrl=dataBuffer.toString().substring(0, dataBuffer.toString().lastIndexOf("&")); //發(fā)送請(qǐng)求 HttpGet get=new HttpGet(resultUrl); //set headers Enumeration headerNames=request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName=headerNames.nextElement(); String headerValue=request.getHeader(headerName); get.setHeader(headerName, headerValue); } HttpResponse rep=client.execute(get); logger.info("開始調(diào)用http請(qǐng)求,請(qǐng)求url:"+resultUrl); //返回結(jié)果 result=EntityUtils.toString(rep.getEntity(),"utf-8"); logger.info(" http 請(qǐng)求調(diào)用結(jié)束!!"); return result; }
看完上述內(nèi)容,你們對(duì)http請(qǐng)求怎么在spring boot中使用有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。