您好,登錄后才能下訂單哦!
這篇文章主要講解了“使用apache http client調(diào)用其他服務(wù)器接口時報錯怎么辦”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“使用apache http client調(diào)用其他服務(wù)器接口時報錯怎么辦”吧!
今天在使用 apache http client 調(diào)用 其他服務(wù)器的接口的時候, get 請求報錯了
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 '/'
說是不支持 header 的 accept 類型。 因為這個 服務(wù)器的接口默認(rèn)只支持返回 json 格式的。所以報錯了,修改 http client 的請求header 的 acept 即可
代碼如下:
/** * GET方式提交數(shù)據(jù) * * @param url 待請求的URL * @param params 要提交的數(shù)據(jù) * @param enc 編碼 * @param resEnc 響應(yīng)內(nèi)容的編碼 * @return 響應(yīng)結(jié)果 */ public static String doGet(String url, Map<String, String> params, 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請求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é)議不對或者返回的內(nèi)容有問題" + 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è)置接收所有類型的,否則如果請求的服務(wù)器只支持 application/json 那么就會報錯 pmethod.addHeader("Accept", "*/*"); return pmethod; }
改為 pmethod.addHeader("Accept", "*/*"); 即可
以上的說法是錯的。
從報錯的信息就可以看出, 是 */ 這種寫法 錯誤的。導(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ù)器接口時報錯怎么辦”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對使用apache http client調(diào)用其他服務(wù)器接口時報錯怎么辦這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。