MIDP 規範強制支援 HTTP 協定,使其成為跨裝置相容性最佳的網路選項。本文詳細比較 GET 與 POST 兩種請求方式的差異、適用場景與 J2ME 實作細節。
參數編碼在 URL 查詢字串中:
GET /login.jsp?user=joe&pass=1234 HTTP/1.1
Host: www.example.com
User-Agent: MIDP/2.0
J2ME 實作:
String url = "https://www.example.com/login.jsp?user=joe&pass=1234";
HttpConnection conn = (HttpConnection) Connector.open(url);
conn.setRequestProperty("User-Agent", "MyApp/1.0");
int rc = conn.getResponseCode();
// 處理回應...
參數置於 HTTP 實體主體,需設定 Content-Type 與 Content-Length:
POST /login.jsp HTTP/1.1
Host: www.example.com
User-Agent: MIDP/2.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 27
user=joe&pass=1234
J2ME 實作:
String url = "https://www.example.com/login.jsp";
String data = "user=joe&pass=1234";
byte[] body = data.getBytes();
HttpConnection conn = (HttpConnection) Connector.open(url);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent", "MyApp/1.0");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(body.length));
OutputStream os = conn.openOutputStream();
os.write(body);
os.flush();
int rc = conn.getResponseCode();
// 處理回應...
| 特性 | GET | POST |
|---|---|---|
| 參數位置 | URL 查詢字串 | HTTP Body |
| 資料長度 | 受限於 URL 長度 (~2KB) | 理論無上限 |
| 資料類型 | 僅文字 | 支援二進位、檔案、多部分表單 |
| 快取行為 | 瀏覽器/Proxy 可能快取 | 通常不快取 |
| 安全性 | 參數可見於記錄/歷史 | 稍佳(但仍需 HTTPS) |
| 書籤/分享 | 可直接複製 URL | 不可 |
| 適用場景 | 查詢、搜尋、冪等操作 | 提交、上傳、狀態變更 |
void postViaHttpConnection(String url) throws IOException {
HttpConnection conn = null;
InputStream is = null;
OutputStream os = null;
try {
conn = (HttpConnection) Connector.open(url);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Language", "zh-TW");
os = conn.openOutputStream();
os.write("param1=value1¶m2=value2".getBytes());
os.flush();
is = conn.openInputStream();
String type = conn.getType();
long len = conn.getLength();
if (len > 0) {
byte[] data = new byte[(int)len];
is.read(data);
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) process((byte)ch);
}
} finally {
closeQuietly(is);
closeQuietly(os);
closeQuietly(conn);
}
}
上傳檔案需構建多部分主體:
String BOUNDARY = "----MobileBoundary" + System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
// 文字欄位
sb.append("--").append(BOUNDARY).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"title\"\r\n\r\n");
sb.append("My Photo").append("\r\n");
// 檔案欄位
sb.append("--").append(BOUNDARY).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"photo.jpg\"\r\n");
sb.append("Content-Type: image/jpeg\r\n\r\n");
byte[] header = sb.toString().getBytes();
byte[] footer = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
conn.setRequestProperty("Content-Length",
String.valueOf(header.length + fileBytes.length + footer.length));
OutputStream os = conn.openOutputStream();
os.write(header);
os.write(fileBytes);
os.write(footer);
os.flush();
User-Agent 識別客戶端版本Content-Type 與 Content-Lengthfinally 區塊確保連線關閉理解 HTTP 協定細節並掌握 J2ME 實作技巧,能讓您開發出穩定、高效、相容性佳的行動網路應用程式。