package com.test.httpurl
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
@Service
public class HttpConnectionReader {
public String getURLBufferedReader(String url, String encoding) {
String sInputData = “”;
String sInputLine = “”;
BufferedReader in = null;
try {
URL ocu = new URL(url);
URLConnection con = ocu.openConnection();
con.setConnectTimeout(2000);
if (encoding.equals(“”)) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(con.getInputStream(),encoding));
}
while ((sInputLine = in.readLine()) != null) {
sInputData += sInputLine + “n”;
}
return sInputData;
}catch(Exception ex){
//System.out.println(“[API]-” + ex);
}finally{
if( in != null ) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//System.out.println(“[API][CLOSE ERROR]-” + e.toString());
}
}
}
return “”;
}
public JSONObject toJSON(String input) {
return new JSONObject(input);
}
}