Skip to content

Instantly share code, notes, and snippets.

@jsfan3
Created June 28, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsfan3/f1b224635f5e13f6ed17015da15e7d7f to your computer and use it in GitHub Desktop.
Save jsfan3/f1b224635f5e13f6ed17015da15e7d7f to your computer and use it in GitHub Desktop.
Spring Boot: URL to JSON. This solution solves 403 and 1010 errors
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
[...]
public String urlToJson(String url) throws IOException {
HttpGet request = new HttpGet(url);
// add request headers
request.addHeader(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
CloseableHttpResponse response = httpClient.execute(request);
// Get HttpResponse Status
String responseStatus = response.getStatusLine().toString();
HttpEntity entity = response.getEntity();
if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
// System.out.println(result);
return result;
} else {
throw new IllegalStateException("Response status: " + responseStatus + " in the request to " + url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment