Search code examples
javaspringinputstreamgoogle-search-appliance

Http 502 Error when trying to get InputStream in my Spring Project


I am using a 3rd party Java API to get results from Google Search Appliance in my Spring based web project. The URL the API constructed seem correct. I've sent a request on web browser with generated URL by API and it worked (search results returned correctly). Moreover, I've tried the URL with standalone Java project that I created to get InputStream and it worked too. The problem comes out when I try to get results in my project. It crashes on the stage of creating InputStream and gives this error:

java.io.IOException: Server returned HTTP response code: 502 for URL: http://myIP:80/search?access=p&output=xml&client=default_frontend&lr=lang_en&num=100&requiredfields=gsaentity_language%3AEnglish&site=default_site
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
    at java.net.URL.openStream(URL.java:1037)
    at net.sf.gsaapi.GSAClient.search(Unknown Source)
    at net.sf.gsaapi.GSAClient.getGSAResponse(Unknown Source)
    at net.sf.gsaapi.GSAClient.getGSAResponse(Unknown Source)

The things I've tried that didn't work for me so far:

  • I've used the API's interface (overriding) to handle HttpConnection and creating InputStream manually. Via this interface, some proxy options can be added to HttpConnection. (The interface named GSAClientDelegate with method getResponseStream)
  • I've checked API's codes in case of finding something that causing proxy trouble etc. I couldn't find any something like that.
  • As stated on an answer, I've checked collection, frontend,proxystylesheet names and they are ok. Otherwise I couldn't reach the results via web browser or standalone Java project right?

It's been almost 1 week I keep facing this annoying and mysterious error and I'm starting to go mad.

What can I do about this issue?


Solution

  • I've finally figured out the base of the problem. It is because of the java.net.HttpUrlConnection API. It uses shared connection factory, which may cause the errors like this. I've replaced it with Apache HttpClient. And no problem showed up:

        import org.apache.http.HttpResponse;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.methods.HttpGet;
        .........
    public InputStream getConnectionStream(String fullUrl){
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(fullUrl);
            HttpResponse response = client.execute(request);
            InputStream is = response.getEntity().getContent();
    
            return is;
    }