public InputStream makeRequest(String url, OutputStream pos) throws IOException { HttpConnection conn = null; ByteArrayOutputStream bos = null; DataInputStream is = null; DataOutputStream os = null; InputStream pis = null; try { conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestProperty("Accept", "application/octet-stream"); if (sessionCookie == null) { conn.setRequestProperty("version", "???"); } else { conn.setRequestProperty("cookie", sessionCookie); } // Getting the output stream may flush the headers os = conn.openDataOutputStream(); os.write(pos.getBytes()); os.close(); int responseCode; try { responseCode = conn.getResponseCode(); } catch (IOException e) { throw new IOException("No response from " + url); } if (responseCode != HttpConnection.HTTP_OK) { throw new IllegalArgumentException(); } bos = new ByteArrayOutputStream(); { is = conn.openDataInputStream(); String sc = conn.getHeaderField("set-cookie"); if (sc != null) { sessionCookie = sc; } while (true) { int ch = is.read(); if (ch == -1) break; bos.write(ch); } is.close(); is = null; conn.close(); conn = null; } pis = new InputStream(bos.toByteArray()); bos.close(); bos = null; } catch (Exception e) { e.printStackTrace(); try { if (conn != null) { conn.close(); conn = null; } if (bos != null) { bos.close(); bos = null; } if (is != null) { is.close(); is = null; } } catch (Exception exc) { } throw new IOException(); } return pis; }
String postRequest(String url) { HttpConnection hc = null; DataInputStream in = null; OutputStream os = null; try { hc = (HttpConnection) Connector.open(url); hc.setRequestMethod(HttpConnection.POST); hc.setRequestProperty("username", "bng"); hc.setRequestProperty("password", "123456"); os = hc.openDataOutputStream(); JSONObject jSONObject = new JSONObject(); jSONObject.put("start_count", 1); jSONObject.put("end_count", 10); jSONObject.put("type", 1); // os.write("{\"start_count\":\"1\",\"end_count\":\"12\"}".getBytes()); os.write(jSONObject.toString().getBytes()); // Always get the Response code first . int responseCode = hc.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { // You have successfully connected. int length = (int) hc.getLength(); System.out.println("length : " + length); byte[] data = null; if (length != -1) { data = new byte[length]; in = new DataInputStream(hc.openInputStream()); in.readFully(data); } else { // If content length is not given, read in chunks. int chunkSize = 512; int index = 0; int readLength = 0; in = new DataInputStream(hc.openInputStream()); data = new byte[chunkSize]; do { if (data.length < index + chunkSize) { byte[] newData = new byte[index + chunkSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; } readLength = in.read(data, index, chunkSize); index += readLength; } while (readLength == chunkSize); length = index; } return new String(data); // Image image = Image.createImage(data, 0, length); // ImageItem imageItem = new ImageItem(null, image, 0, null); // mForm.append(imageItem); // mForm.setTitle("Done."); } else { // Problem with your connection Dialog.show(null, "error downloading images", "OK", null); return null; } } catch (Exception e) { e.printStackTrace(); Dialog.show(null, "error downloading images", "OK", null); return null; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (hc != null) { try { hc.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }