int cut = url.indexOf('#');
// System.out.println("");
// System.out.println("Requesting: " + url);
StreamConnection con = (StreamConnection) Connector.open(
cut == -1 ? url : url.substring(0, cut),
post ? Connector.READ_WRITE : Connector.READ);
screen.setStatus(type == SystemRequestHandler.TYPE_IMAGE ? "Loading Image" :
"Requesting");
int contentLength = -1;
if (con instanceof HttpConnection) {
HttpConnection httpCon = (HttpConnection) con;
if (post) {
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}
httpCon.setRequestProperty("User-Agent", browser.userAgent);
httpCon.setRequestProperty("X-Screen-Width", ""+screen.getWidth());
httpCon.setRequestProperty("Connection", "Keep-Alive");
httpCon.setRequestProperty("Accept-Charset", "utf-8");
String cookie = browser.getCookies(url);
if (cookie != null) {
// System.out.println("Setting Cookie: " + cookie);
httpCon.setRequestProperty("Cookie", cookie);
}
if (post && requestData != null) {
OutputStream os = httpCon.openOutputStream();
os.write(requestData);
os.close();
// System.out.println("Postdata\n" + new String(requestData));
}
// System.out.println("receiving:");
contentLength = (int) httpCon.getLength();
int headerIndex = 0;
while (true){
String name = httpCon.getHeaderFieldKey(headerIndex);
if (name == null) {
break;
}
name = name.toLowerCase();
// System.out.println(name + ": " + httpCon.getHeaderField(headerIndex));
if ("content-type".equals(name)) {
String value = httpCon.getHeaderField(headerIndex);
if (value.indexOf("vnd.sun.j2me.app-descriptor") != -1) {
browser.platformRequest(url);
return;
}
int eq = value.indexOf ("harset=");
if (eq != -1) {
encoding = value.substring(eq + 7).trim();
}
} else if ("set-cookie".equals(name)) {
String cName = "";
String cValue = "";
String cHost = httpCon.getHost();
String cPath = "";
String cExpires = null;
String cSecure = null;
String[] parts = Util.split(
httpCon.getHeaderField(headerIndex), ';');
for (int i = 0; i < parts.length; i++){
String part = parts[i];
cut = part.indexOf('=');
if (cut == -1) {
continue;
}
String key = part.substring(0, cut).trim();
String value = part.substring(cut + 1).trim();
if (i == 0) {
cName = key;
cValue = value;
} else {
key = key.toLowerCase();
if ("host".equals(key)) {
cHost = value;
} else if ("path".equals(key)) {
cPath = value;
} else if ("expires".equals(key)) {
cExpires = value;
} else if ("secure".equals(key)) {
cSecure = value;
}
}
}
browser.setCookie(cHost, cPath, cName, cValue, cExpires, cSecure);
}
headerIndex++;
}
int responseCode = httpCon.getResponseCode();
// System.out.println("Response Code: " + httpCon.getResponseCode());
if (responseCode >= 300 && responseCode <= 310) {
String location = httpCon.getHeaderField("Location");
if (location != null) {
// System.out.println("Redirecting to: " + location);
screen.htmlWidget.setUrl(location);
browser.requestResource(screen.htmlWidget, method, location,
type, requestData);
return;
}
}
if (responseCode != 200 && !page) {
throw new IOException("HTTP Error " + responseCode);
}
}
byte [] responseData;
InputStream is = con.openInputStream();
if (is == null) {
responseData = new byte[0];
} else {
if (contentLength == -1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte [] buf = new byte [2048];
while (true) {
screen.setStatus(total / 1000 + "k");
int len = is.read(buf, 0, 2048);
if (len <= 0) {
break;
}
total += len;
baos.write(buf, 0, len);
}
responseData = baos.toByteArray();
} else {
responseData = new byte[contentLength];
int pos = 0;
while (pos < contentLength) {
screen.setStatus(total / 1000 + "k");
int len = is.read(responseData, pos,
Math.min(contentLength - pos, 4096));
if (len <= 0) {
break;
}
pos += len;
total += len;
}
}
}
con.close();
switch (type) {
case SystemRequestHandler.TYPE_DOCUMENT:
String tmp = new String(responseData, 0, Math.min(responseData.length,