Newer
Older
package org.josast.propertyHttp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Logger;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
/**
* @author christophe
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
private static Logger log = Logger.getLogger("AmsatLogger");
private String url = "";
private Properties props = new Properties();
private boolean connected = false;
/**
* @param url URL of the properties file to read
*/
public ConfigHttp(final String url) {
super();
this.url = url;
try {
loadProperties();
} catch (Exception e) {
log.severe(e.getMessage());
}
}
/**
* Load the properties file and initialise Properties field
*/
private void loadProperties() {
final String USER_AGENT = "Mozilla/5.0";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response;
try {
response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
props.load(rd);
connected = true;
} catch (ClientProtocolException e) {
log.severe("Error read URL");
} catch (IOException e) {
log.severe("Error read URL ");
}
}
/**
*
* @param Item property name
* @return property value, null if property not found
*/
public String GetProperty(final String Item) {
if (props != null) {
String S = props.getProperty(Item);
if (S == null) {
return null;
} else {
return S.trim(); // $NON-NLS-1$
}
}
return null;
}
/**
* @return true if the Property file has been read
*/
public boolean isConnected() {
return connected;
}
public static void main(String[] args) {
ConfigHttp c = new ConfigHttp(
"http://site.amsat-f.org/download/117168/");
System.out.println(c.GetProperty("EntrySatVersion"));
}