SocketConnection 要签名,实在没办法就先用 HttpConnection 将就用着吧~ /** * Description: * Simple HTTP Client for CMWAP * * History: * Written by Moky @ 2009-08-07 */ /* // Usgae: String http_request = "POST / HTTP/1.1/r/n" + "Host: www.moky.net.cn/r/n" + "X-Online-Host: www.moky.net.cn/r/n" + "Content-Length: 9/r/n" + "Content-Type: application/x-www-form-urlencoded/r/n" + "Connection: keep-alive/r/n" + "/r/n" + "qq=660260"; SimpleHttpClient client = new SimpleHttpClient(); String http_response = client.request(http_request); */ package cn.net.moky.lib.http; import java.io.*; import javax.microedition.io.*; public class SimpleHttpClient { private static final String PROXY_HOST = "10.0.0.172"; private HttpConnection conn = null; public String request(String req) { String res = SendRecv(req); if (res.toLowerCase().indexOf("onenterforward") > 0) { int pB = res.toLowerCase().indexOf("<go hr" + "ef=/""); if (pB > 0) { pB += 10; int pE = res.indexOf("/">", pB); if (pE > 0) { String uri = res.substring(pB, pE); int p = uri.indexOf(PROXY_HOST); if (p > 0) { p += 10; p = uri.indexOf("/", p); uri = uri.substring(p); } pB = req.indexOf(" "); pB++; pE = req.indexOf(" ", pB); String pack = req.substring(0, pB) + uri + req.substring(pE); res = SendRecv(pack); } } } return res; } //------------- private int open(String URI, boolean post) { try { if (conn != null) { //conn.disconnect(); conn = null; } String url = "http://" + PROXY_HOST + URI; conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE); if (post) { conn.setRequestMethod(conn.POST); } } catch(Exception e) { return -1; } return 0; } private int setHeader(String key, String value) { if (conn == null) return -1; try { conn.setRequestProperty(key, value); } catch(Exception e) { return -1; } return 0; } private String getResponseHeaders() { String key, value; String headers = ""; try { headers = conn.getResponseCode() + " " + conn.getResponseMessage() + "/r/n"; for (int x = 0; x < 32; x++) { key = conn.getHeaderFieldKey(x); if (key == null) continue; value = conn.getHeaderField(key); headers += key + ": " + value + "/r/n"; } } catch(Exception e) { } return headers; } private String SendRecv(String req) { String recv = "receiving..."; try { if (req == null) return null; String[] tmpTwo = split(req, "/r/n/r/n", 2); if (tmpTwo.length < 1) return null; String[] tmp = split(tmpTwo[0], "/r/n"); for (int x = 0; x < tmp.length; x++) { String line = tmp[x]; if (line.toUpperCase().startsWith("GET ") || line.toUpperCase().startsWith("POST ")) { String[] arr = split(line, " "); open(arr[1], arr[0].toUpperCase() == "POST"); continue; } String[] arr = split(line, ":", 2); if (arr.length != 2) continue; setHeader(arr[0].trim(), arr[1].trim()); } DataOutputStream dos = conn.openDataOutputStream(); if (tmpTwo.length > 1) { dos.write(tmpTwo[1].getBytes()); } dos.flush(); dos.close(); int code = conn.getResponseCode(); //if (code != HttpConnection.HTTP_OK) { //} recv = getResponseHeaders() + "/r/n"; DataInputStream dis = conn.openDataInputStream(); byte[] buf = new byte[128]; while (true) { int len = dis.read(buf, 0, buf.length); if (len == -1) break; recv += new String(buf, 0, len); } dis.close(); } catch(Exception e) { } return recv; } private String[] split(String src, String seperator) { return split(src, seperator, 100); } private String[] split(String src, String seperator, int limit) { int p1, p2 = 0; String[] arr = new String[limit]; int x = 0; while ((p1 = src.indexOf(seperator, p2)) != -1) { if (limit > 0 && x >= limit - 1) break; arr[x] = src.substring(p2, p1); x++; p2 = p1 + seperator.length(); } arr[x] = src.substring(p2); x++; String[] ret = new String[x]; System.arraycopy(arr, 0, ret, 0, x); return ret; } }