Created
June 28, 2020 16:51
-
-
Save jsfan3/f1b224635f5e13f6ed17015da15e7d7f to your computer and use it in GitHub Desktop.
Spring Boot: URL to JSON. This solution solves 403 and 1010 errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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