private String postViaHttpConnection(String url, byte[] b) throws IOException { HttpConnection c = null; InputStream is = null; OutputStream os = null; int rc; try { c = (HttpConnection) Connector.open(url); // Set the request method and headers c.setRequestMethod(HttpConnection.POST); c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT"); c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language", "en-US"); // Getting the output stream may flush the headers os = c.openOutputStream(); os.write(b); os.flush(); // Optional, getResponseCode will flush // Getting the response code will open the connection, // send the request, and read the HTTP response headers. // The headers are stored until requested. rc = c.getResponseCode(); if (rc != HttpConnection.HTTP_OK) { throw new IOException("HTTP response code: " + rc); } is = c.openInputStream(); // Get the ContentType String type = c.getType(); // processType(type); // Get the length and process the data int len = (int) c.getLength(); if (len > 0) { int actual = 0; int bytesread = 0; byte[] data = new byte[len]; while ((bytesread != len) && (actual != -1)) { actual = is.read(data, bytesread, len - bytesread); bytesread += actual; } String s = ""; for (int i = 0; i < data.length; i++) { s = s + (char) data[i]; } return s; } // else { // int ch; // while ((ch = is.read()) != -1) { // process((byte)ch); // } // } } catch (ClassCastException e) { throw new IllegalArgumentException("Not an HTTP URL"); } finally { if (is != null) is.close(); if (os != null) os.close(); if (c != null) c.close(); } return "Resp"; // }
/** * Gets the connection attribute of the CommonDS object * * @exception IOException Description of the Exception */ void getConnection() throws IOException { boolean goodurl = false; if (locator == null) { throw (new IOException(this + ": connect() failed")); } contentType = null; String fileName = getRemainder(locator); if (fileName == null) { throw new IOException("bad url"); } int i = fileName.lastIndexOf((int) ('.')); if (i != -1) { String ext = fileName.substring(i + 1).toLowerCase(); contentType = Configuration.getConfiguration().ext2Mime(ext); } if (contentType == null) { contentType = "unknown"; } try { if (locator.toLowerCase().startsWith("http:")) { HttpConnection httpCon = (HttpConnection) Connector.open(locator); int rescode = httpCon.getResponseCode(); // If the response code of HttpConnection is in the range of // 4XX and 5XX, that means the connection failed. if (rescode >= 400) { httpCon.close(); goodurl = false; } else { inputStream = httpCon.openInputStream(); contentLength = httpCon.getLength(); String ct = httpCon.getType().toLowerCase(); if (contentType.equals("unknown")) { contentType = Configuration.getConfiguration().ext2Mime(ct); } httpCon.close(); goodurl = true; } } else if (locator.startsWith("file:")) { // #ifdef USE_FILE_CONNECTION [ FileConnection fileCon = (FileConnection) Connector.open(locator); if (fileCon.exists() && !fileCon.isDirectory() && fileCon.canRead()) { inputStream = fileCon.openInputStream(); contentLength = fileCon.fileSize(); fileCon.close(); goodurl = true; } else { fileCon.close(); goodurl = false; } // #else ] [ // throw new IOException("file protocol isn't supported"); // #endif ] } else if (locator.startsWith("rtp:")) { if (contentType.equals("unknown")) contentType = "content.rtp"; inputStream = null; contentLength = -1; goodurl = true; } else if (locator.startsWith("rtsp:")) { if (contentType.equals("unknown")) contentType = "content.rtp"; inputStream = null; contentLength = -1; goodurl = true; } else if (locator.equals(Manager.TONE_DEVICE_LOCATOR) // #ifndef ABB [ // || locator.equals(Manager.MIDI_DEVICE_LOCATOR) // #endif ] ) { inputStream = null; contentLength = -1; goodurl = true; } } catch (Exception ex) { ex.printStackTrace(); throw new IOException("failed to connect: " + ex.getMessage()); } if (!goodurl) throw new IOException("bad url"); }