byte[] data =.....; //发送请求request conn.setRequestProperty("Content-Length", Integer.toString(data.length)); OutputStream os = conn.openOutputStream(); os.write(data); os.close();
//以下是接受response int rc = conn.getResponseCode(); if (rc == HttpConnection.HTTP_OK) { int len = (int)conn.getLength(); InputStream in = conn.openInputStream(); if (len != -1) { int total = 0; data = new byte[len]; while (total < len) { total += in.read(data, total, len - total); } } else { ByteArrayOutputStream tmp = new ByteArrayOutputStream(); int ch; while ((ch = in.read()) != -1) { tmp.write(ch); } data = tmp.toByteArray(); }