十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章主要講解了“使用apache http client調(diào)用其他服務(wù)器接口時(shí)報(bào)錯(cuò)怎么辦”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“使用apache http client調(diào)用其他服務(wù)器接口時(shí)報(bào)錯(cuò)怎么辦”吧!
成都創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)營(yíng)銷(xiāo)型網(wǎng)站、網(wǎng)站重做改版、虞城網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為虞城等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
今天在使用 apache http client 調(diào)用 其他服務(wù)器的接口的時(shí)候, get 請(qǐng)求報(bào)錯(cuò)了
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not parse 'Accept' header [text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8]: Invalid mime type "*/;q=0.8": does not contain subtype after '/' org.springframework.util.InvalidMimeTypeException: Invalid mime type "*/;q=0.8": does not contain subtype after '/'
說(shuō)是不支持 header 的 accept 類(lèi)型。 因?yàn)檫@個(gè) 服務(wù)器的接口默認(rèn)只支持返回 json 格式的。所以報(bào)錯(cuò)了,修改 http client 的請(qǐng)求header 的 acept 即可
代碼如下:
/** * GET方式提交數(shù)據(jù) * * @param url 待請(qǐng)求的URL * @param params 要提交的數(shù)據(jù) * @param enc 編碼 * @param resEnc 響應(yīng)內(nèi)容的編碼 * @return 響應(yīng)結(jié)果 */ public static String doGet(String url, Mapparams, String enc, String resEnc) { String response = EMPTY; HttpGet getMethod = null; if (StringUtils.isEmpty(url)) { return null; } StringBuffer strtTotalURL = getTotalUrl(url, params, enc); logger.debug("GET請(qǐng)求URL = \n" + strtTotalURL.toString()); try { getMethod = getGetMethod(strtTotalURL.toString()); getMethod.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc); // 執(zhí)行g(shù)etMethod HttpResponse httpResponse = getHttpClient(url).execute(getMethod); response = getResponse(url, httpResponse, resEnc); } catch (ClientProtocolException e) { logger.error("發(fā)生致命的異常,可能是協(xié)議不對(duì)或者返回的內(nèi)容有問(wèn)題" + e.getMessage(), e); } catch (IOException e) { logger.error("發(fā)生網(wǎng)絡(luò)異常" + e.getMessage(), e); } finally { if (getMethod != null) { getMethod.releaseConnection(); getMethod = null; } } return response; } /** * 模擬瀏覽器GET提交 * * @param url * @return */ private static HttpGet getGetMethod(String url) { if (!url.startsWith(HTTP)) { url = "http://" + url; } HttpGet pmethod = new HttpGet(url); // 設(shè)置響應(yīng)頭信息 pmethod.addHeader("Connection", "keep-alive"); pmethod.addHeader("Cache-Control", "max-age=0"); pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); // pmethod.addHeader("Accept", // "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8"); // 設(shè)置接收所有類(lèi)型的,否則如果請(qǐng)求的服務(wù)器只支持 application/json 那么就會(huì)報(bào)錯(cuò) pmethod.addHeader("Accept", "*/*"); return pmethod; }
改為 pmethod.addHeader("Accept", "*/*"); 即可
以上的說(shuō)法是錯(cuò)的。
從報(bào)錯(cuò)的信息就可以看出, 是 */ 這種寫(xiě)法 錯(cuò)誤的。導(dǎo)致header accept 解析不成功。
改為
pmethod.addHeader( "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
完整版
pmethod.addHeader( "Accept", "text/html,application/xhtml+xml,application/xml;application/json,*/*;q=0.9,*/*;q=0.8");
感謝各位的閱讀,以上就是“使用apache http client調(diào)用其他服務(wù)器接口時(shí)報(bào)錯(cuò)怎么辦”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)使用apache http client調(diào)用其他服務(wù)器接口時(shí)報(bào)錯(cuò)怎么辦這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!