Skip to content

Commit dd7ecde

Browse files
committed
added new apacheHttpGET method to Connectors
1 parent f5d1574 commit dd7ecde

File tree

1 file changed

+78
-27
lines changed

1 file changed

+78
-27
lines changed

src/main/java/net/b07z/sepia/server/core/tools/Connectors.java

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2828
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
2929
import org.apache.http.impl.client.CloseableHttpClient;
30+
import org.apache.http.impl.client.HttpClientBuilder;
3031
import org.apache.http.impl.client.HttpClients;
3132
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
3233
import org.apache.http.ssl.SSLContexts;
@@ -57,14 +58,20 @@ public enum Method {
5758
delete,
5859
head
5960
}
60-
static class HttpClientResult {
61-
int statusCode = 0;
62-
String content = "";
61+
public static class HttpClientResult {
62+
public int statusCode = 0;
63+
public String statusLine = "";
64+
public String content = "";
6365

6466
HttpClientResult(String content, int statusCode){
6567
this.content = content;
6668
this.statusCode = statusCode;
6769
}
70+
HttpClientResult(String content, int statusCode, String statusLine){
71+
this.content = content;
72+
this.statusCode = statusCode;
73+
this.statusLine = statusLine;
74+
}
6875
}
6976

7077
/**
@@ -96,7 +103,34 @@ public static JSONObject simpleJsonGet(String url) {
96103
}
97104
}
98105
/**
99-
* Simple HTTP GET. Basically the same as "simpleHtmlGet" just realized with Apache HTTP client.
106+
* Sends a GET and returns result as string. NOTE: sets content-type to: 'text/html'.
107+
* Throws a RuntimeException on fail.
108+
*/
109+
public static String simpleHtmlGet(String url) {
110+
try {
111+
URL urlObj = new URL(url);
112+
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
113+
con.setRequestMethod("GET");
114+
con.setRequestProperty("User-Agent", USER_AGENT);
115+
con.setRequestProperty("content-type", "text/html");
116+
con.setConnectTimeout(CONNECT_TIMEOUT);
117+
con.setReadTimeout(READ_TIMEOUT);
118+
int responseCode = con.getResponseCode();
119+
if (responseCode == HttpURLConnection.HTTP_OK){
120+
try (InputStream stream = con.getInputStream();
121+
InputStreamReader isr = new InputStreamReader(stream, Charsets.UTF_8)) {
122+
String content = CharStreams.toString(isr);
123+
return content;
124+
}
125+
} else {
126+
throw new RuntimeException(DateTime.getLogDate() + " ERROR - Could not get '" + url + "': response code " + responseCode);
127+
}
128+
} catch (Exception e) {
129+
throw new RuntimeException(DateTime.getLogDate() + " ERROR - Could not get '" + url + "', error: " + e.getMessage(), e);
130+
}
131+
}
132+
/**
133+
* Simple HTTP GET. Basically the same as "simpleJsonGet" just realized with Apache HTTP client.
100134
* @param url - URL to call
101135
* @return JSONObject
102136
*/
@@ -121,30 +155,47 @@ public static JSONObject apacheHttpGETjson(String url) throws Exception{
121155
}
122156
}
123157
/**
124-
* Sends a GET and returns result as string.
125-
* Throws a RuntimeException on fail.
158+
* Simple HTTP GET. Basically the same as "simpleHtmlGet" just realized with Apache HTTP client and allows to set
159+
* or skip custom content-type. Probably the most reliable GET version in this package if you need a string in return.<br>
160+
* NOTE: If the URL was redirected it will produce a ERROR log message and NOT follow the link.<br>
161+
* NOTE2: Cookie management is disabled
162+
* @param url - URL to call
163+
* @param contentType - null for 'auto' or e.g. 'application/json' or 'application/rss+xml' etc.
164+
* @return JSONObject
126165
*/
127-
public static String simpleHtmlGet(String url) {
128-
try {
129-
URL urlObj = new URL(url);
130-
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
131-
con.setRequestMethod("GET");
132-
con.setRequestProperty("User-Agent", USER_AGENT);
133-
con.setRequestProperty("content-type", "text/html");
134-
con.setConnectTimeout(CONNECT_TIMEOUT);
135-
con.setReadTimeout(READ_TIMEOUT);
136-
int responseCode = con.getResponseCode();
137-
if (responseCode == HttpURLConnection.HTTP_OK){
138-
try (InputStream stream = con.getInputStream();
139-
InputStreamReader isr = new InputStreamReader(stream, Charsets.UTF_8)) {
140-
String content = CharStreams.toString(isr);
141-
return content;
142-
}
143-
} else {
144-
throw new RuntimeException(DateTime.getLogDate() + " ERROR - Could not get '" + url + "': response code " + responseCode);
145-
}
146-
} catch (Exception e) {
147-
throw new RuntimeException(DateTime.getLogDate() + " ERROR - Could not get '" + url + "', error: " + e.getMessage(), e);
166+
public static HttpClientResult apacheHttpGET(String url, String contentType) throws Exception{
167+
CloseableHttpClient httpclient = HttpClientBuilder.create()
168+
.disableRedirectHandling()
169+
.disableCookieManagement()
170+
.build();
171+
//CloseableHttpClient httpclient = HttpClients.createDefault();
172+
HttpGet httpGet = new HttpGet(url);
173+
if (contentType != null && !contentType.isEmpty()){
174+
httpGet.setHeader("content-type", contentType);
175+
}
176+
httpGet.addHeader("User-Agent", USER_AGENT);
177+
String statusLine = "";
178+
int statusCode = 0;
179+
String responseData = null;
180+
try (CloseableHttpResponse response = httpclient.execute(httpGet);){
181+
statusLine = response.getStatusLine().toString();
182+
statusCode = response.getStatusLine().getStatusCode();
183+
//System.out.println(statusLine);
184+
if (statusCode == 301){
185+
String errorRedirect = response.getFirstHeader("Location").getValue();
186+
statusLine += (", NEW URI: " + errorRedirect);
187+
Debugger.println("HTTP GET: '" + url + "' has REDIRECT to: " + errorRedirect, 1);
188+
}else{
189+
HttpEntity resEntity = response.getEntity();
190+
if (resEntity != null){
191+
responseData = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
192+
}
193+
EntityUtils.consume(resEntity);
194+
}
195+
return new HttpClientResult(responseData, statusCode, statusLine);
196+
197+
}catch (Exception e){
198+
return new HttpClientResult(null, statusCode, statusLine);
148199
}
149200
}
150201

0 commit comments

Comments
 (0)