java.io.IOException: Server returned HTTP response code: 502 for URL
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?
First, this is a 5xx error, so the issue does not (generally) lie on any system that you control. If you're consistently getting this error, then the third-party API provider you're interfacing with is having technical difficulties, and you should reach out to them as soon as possible.
Since you may be going through a gateway or proxy, consider using a direct connection to the resource that you need.
You most likely made a typo in a parameter of your query.
A 502 error usually means you try to load a frontend (so check client / proxystylesheet parameter) or a collection (check parameter site. default_site ? Real default is default_collection if you didn't made a new one) that doesn't exist.
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;
}