Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Using apache http client to call other server interfaces, what if there is an error?

Shulou Source: shulou.com Published: 2022-06-01 06:43:10 10月03日 Update

This article mainly explains how to use apache http client to call other server interfaces. The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's ideas to study and learn how to use apache http client to call other server interfaces.

Today, when using apache http client to call the interfaces of other servers, the get request reported an error

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 "* /; qroom0.8": does not contain subtype after' / 'org.springframework.util.InvalidMimeTypeException: Invalid mime type "* /; qroom0.8": does not contain subtype after' /'

Said that the accept type of header is not supported. Because the interface of this server only supports returning json format by default. Therefore, an error has been reported. Just modify the acept of the http client request header.

The code is as follows:

/ * data submitted by GET * * @ param url data to be submitted by URL * @ param params to be submitted * @ param enc Encoding * @ param resEnc response content Encoding * @ return response result * / public static String doGet (String url, Map 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 request URL =\ n" + strtTotalURL.toString ()); try {getMethod = getGetMethod (strtTotalURL.toString ()); getMethod.setHeader ("Content-Type", "application/x-www-form-urlencoded;charset=" + enc); / / execute getMethod HttpResponse httpResponse = getHttpClient (url) .execute (getMethod); response = getResponse (url, httpResponse, resEnc) } catch (ClientProtocolException e) {logger.error ("fatal exception occurs, maybe the protocol is incorrect or there is a problem with the returned content" + e.getMessage (), e);} catch (IOException e) {logger.error ("network exception" + e.getMessage (), e);} finally {if (getMethod! = null) {getMethod.releaseConnection (); getMethod = null }} return response;} / * Analog browser GET submit * * @ param url * @ return * / private static HttpGet getGetMethod (String url) {if (! url.startsWith (HTTP)) {url = "http://" + url;} HttpGet pmethod = new HttpGet (url); / / set response header 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") / / set to receive all types, otherwise pmethod.addHeader ("Accept", "* / *") will be reported if the requested server only supports application/json; return pmethod;}

Change it to pmethod.addHeader ("Accept", "* / *").

Improve

The above statement is wrong.

From the error message, we can see that it is * / this kind of writing is wrong. Causes header accept parsing to be unsuccessful.

Change to

Pmethod.addHeader ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

Full version

Pmethod.addHeader ("Accept", "text/html,application/xhtml+xml,application/xml;application/json,*/*;q=0.9,*/*;q=0.8") Thank you for your reading, the above is the content of "what to do wrong when using apache http client to call other server interfaces". After the study of this article, I believe you have a deeper understanding of how to use apache http client to call other server interfaces. Specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Servers services interfaces what to do times content learning support information data types coding problems no success code writing that is ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Docker OPPO Reno Shulou Technology