refactor
This commit is contained in:
parent
1c327e5832
commit
1b44489b20
@ -9,31 +9,31 @@ import cz.kamma.mining.Constants;
|
||||
|
||||
public class HttpHelper {
|
||||
|
||||
public static String getStringResponse(String urlStr) {
|
||||
return getStringResponse(urlStr, Constants.DEFAULT_HTTP_TIMEOUT);
|
||||
}
|
||||
public static String getStringResponse(String urlStr) {
|
||||
return getStringResponse(urlStr, Constants.DEFAULT_HTTP_TIMEOUT);
|
||||
}
|
||||
|
||||
public static String getStringResponse(String urlStr, int timeout) {
|
||||
String output = "";
|
||||
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(false);
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
|
||||
conn.connect();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null)
|
||||
output += inputLine;
|
||||
in.close();
|
||||
conn.disconnect();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error while getting HTTP response from " + urlStr + ". Ex:" + e);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
public static String getStringResponse(String urlStr, int timeout) {
|
||||
String output = "";
|
||||
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(false);
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setRequestProperty("User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
|
||||
conn.connect();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null)
|
||||
output += inputLine;
|
||||
in.close();
|
||||
conn.disconnect();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error while getting HTTP response from " + urlStr + ". Ex:" + e);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user