10 changed files with 166 additions and 6 deletions
@ -0,0 +1,63 @@
|
||||
package org.leolo.nrdatad.org.leolo.nrdatad.util; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.leolo.nrdatad.ConfigurationManager; |
||||
import org.leolo.nrdatad.Constants; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.net.URISyntaxException; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.util.Base64; |
||||
import java.util.zip.GZIPInputStream; |
||||
|
||||
public class HttpUtil { |
||||
|
||||
Logger log = LogManager.getLogger(); |
||||
|
||||
public static String sendHttpRequest(URL url, String userName, String password) throws IOException { |
||||
URLConnection conn = url.openConnection(); |
||||
String userpwd = userName+":"+password; |
||||
conn.addRequestProperty("Authorization", "Basic "+ Base64.getEncoder().encodeToString(userpwd.getBytes())); |
||||
conn.connect(); |
||||
StringBuilder sb = new StringBuilder(); |
||||
try(BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))){ |
||||
while(true){ |
||||
String line = br.readLine(); |
||||
if(line == null) break; |
||||
sb.append(line).append("\n"); |
||||
} |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
public static String sendHttpRequest(String url, String userName, String password) throws IOException, URISyntaxException { |
||||
return sendHttpRequest(new URL(url), userName, password); |
||||
} |
||||
public static String sendHttpRequestForGZipFile(URL url, String userName, String password) throws IOException { |
||||
URLConnection conn = url.openConnection(); |
||||
String userpwd = userName+":"+password; |
||||
conn.addRequestProperty("Authorization", "Basic "+ Base64.getEncoder().encodeToString(userpwd.getBytes())); |
||||
conn.connect(); |
||||
StringBuilder sb = new StringBuilder(); |
||||
try( |
||||
GZIPInputStream gis = new GZIPInputStream(conn.getInputStream()); |
||||
BufferedReader br = new BufferedReader(new InputStreamReader(gis)) |
||||
){ |
||||
while(true){ |
||||
String line = br.readLine(); |
||||
if(line == null) break; |
||||
sb.append(line).append("\n"); |
||||
} |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
public static String sendHttpRequestForGZipFile(String url, String userName, String password) throws IOException, URISyntaxException { |
||||
return sendHttpRequestForGZipFile(new URL(url), userName, password); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue