1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
class TERMobileBrowser {
public static final class Response {
private Map<String, List<String>> headers = new HashMap<String, List<String>>();
private String body = null;
private Integer statusCode = null;
private String statusDescription = null;
private static final Pattern PATTERN_STATUS = Pattern.compile("^[A-Z/0-9\\.]+ ([0-9]+) (.*)$");
public Response(BufferedReader r) throws IOException {
this(r, true);
}
public Response(BufferedReader r, boolean readBody) throws IOException {
boolean inBody = false;
boolean parsedStatus = false;
String line;
while ((line = r.readLine()) != null) {
if (!inBody) {
line = line.trim();
if (!parsedStatus) {
Matcher m = PATTERN_STATUS.matcher(line);
parsedStatus = true;
if (m.find()) {
statusCode = Integer.parseInt(m.group(1));
statusDescription = m.group(2);
continue;
} else {
statusCode = 0;
statusDescription = null;
}
}
if (line.equals("")) {
if (!readBody) {
break;
} else {
inBody = true;
}
} else {
addHeader(line);
}
} else {
body += line;
}
}
r.close();
}
public void addHeader(String line) {
int sepPos = line.indexOf(':');
String headerName = line.substring(0, sepPos).trim().toLowerCase();
String headerValue = line.substring(sepPos+1).trim();
if (!headers.containsKey(headerName)) {
headers.put(headerName, new ArrayList<String>());
}
headers.get(headerName).add(headerValue);
}
public String getBody() {
return body;
}
public List<String> getHeaderList(String name) {
if (headers.containsKey(name)) {
return headers.get(name);
}
return null;
}
public String getHeader(String name, int index) {
if (headers.containsKey(name)) {
List<String> values = headers.get(name);
if (0 <= index && index < values.size()) {
return values.get(index);
}
}
return null;
}
public String getHeader(String name) {
return getHeader(name, 0);
}
public String[] getHeaderNames() {
return headers.keySet().toArray(new String[0]);
}
public Integer getStatusCode() {
return statusCode;
}
public String getStatusDescription() {
return statusDescription;
}
}
private Map<String, String> getDefaultHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; fr; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
headers.put("Accept-Language", "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3");
headers.put("Accept-Encoding", "gzip,deflate");
headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
headers.put("Keep-Alive", "115");
headers.put("Connection", "keep-alive");
return headers;
}
private Response readURL(String host, int port, String path, Map<String, String> headers) throws IOException {
return readURL(host, port, path, "GET", headers);
}
private Response readURL(String host, int port, String path, String method, Map<String, String> headers) throws IOException {
Socket s = new Socket(host, port);
OutputStreamWriter w = new OutputStreamWriter(s.getOutputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
w.write(method + " " + path + " HTTP/1.1\n");
w.write("Host: " + host + "\n");
for (Map.Entry<String, String> header : headers.entrySet()) {
String name = header.getKey();
String value = header.getValue();
String headerLine = name + ": " + value + "\n";
w.write(headerLine);
}
w.write("\n");
w.flush();
while (!r.ready()) {
// FIXME add timeout
continue;
}
w.close();
return new Response(r, false);
}
private String getSessionCookie() throws IOException {
Map<String, String> headers = getDefaultHeaders();
Response response = readURL("www.termobile.fr", 80, "/pages/imode/accueil.jsp", headers);
return response.getHeader("set-cookie");
}
} |
Partager