/** * READ * * @param url * @return * @throws IOException */ public String get(String url) throws IOException { HttpConnection hcon = null; DataInputStream dis = null; StringBuffer responseMessage = new StringBuffer(); try { int redirectTimes = 0; boolean redirect; do { redirect = false; // a standard HttpConnection with READ access hcon = getConnection(url); // obtain a DataInputStream from the HttpConnection dis = new DataInputStream(hcon.openInputStream()); // retrieve the response from the server int ch; while ((ch = dis.read()) != -1) { responseMessage.append((char) ch); } // end while ( ( ch = dis.read() ) != -1 ) // check status code int status = hcon.getResponseCode(); switch (status) { case HttpConnection.HTTP_OK: // Success! break; case HttpConnection.HTTP_TEMP_REDIRECT: case HttpConnection.HTTP_MOVED_TEMP: case HttpConnection.HTTP_MOVED_PERM: // Redirect: get the new location url = hcon.getHeaderField("location"); System.out.println("Redirect: " + url); if (dis != null) dis.close(); if (hcon != null) hcon.close(); hcon = null; redirectTimes++; redirect = true; break; default: // Error: throw exception hcon.close(); throw new IOException("Response status not OK:" + status); } // max 5 redirects } while (redirect == true && redirectTimes < 5); if (redirectTimes == 5) { throw new IOException("Too much redirects"); } } catch (Exception e) { e.printStackTrace(); // TODO bad style responseMessage.append("ERROR: "); } finally { try { if (hcon != null) hcon.close(); if (dis != null) dis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } // end try/catch } // end try/catch/finally return responseMessage.toString(); } // end sendGetRequest( String )
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; }
/** * UPDATE * * @param url * @param data the request body * @throws IOException */ public String post(String url, String data) throws IOException { HttpConnection hcon = null; DataInputStream dis = null; DataOutputStream dos = null; StringBuffer responseMessage = new StringBuffer(); try { int redirectTimes = 0; boolean redirect; do { redirect = false; // an HttpConnection with both read and write access hcon = getConnection(url, Connector.READ_WRITE); // set the request method to POST hcon.setRequestMethod(HttpConnection.POST); // overwrite content type to be form based hcon.setRequestProperty("Content-Type", "application/json"); // set message length if (data != null) { hcon.setRequestProperty("Content-Length", "" + data.length()); } if (data != null) { // obtain DataOutputStream for sending the request string dos = hcon.openDataOutputStream(); byte[] request_body = data.getBytes(); // send request string to server for (int i = 0; i < request_body.length; i++) { dos.writeByte(request_body[i]); } // end for( int i = 0; i < request_body.length; i++ ) dos.flush(); // Including this line may produce // undesiredresults on certain devices } // obtain DataInputStream for receiving server response dis = new DataInputStream(hcon.openInputStream()); // retrieve the response from server int ch; while ((ch = dis.read()) != -1) { responseMessage.append((char) ch); } // end while( ( ch = dis.read() ) != -1 ) { // check status code int status = hcon.getResponseCode(); switch (status) { case HttpConnection.HTTP_OK: // Success! break; case HttpConnection.HTTP_TEMP_REDIRECT: case HttpConnection.HTTP_MOVED_TEMP: case HttpConnection.HTTP_MOVED_PERM: // Redirect: get the new location url = hcon.getHeaderField("location"); System.out.println("Redirect: " + url); if (dis != null) dis.close(); if (hcon != null) hcon.close(); hcon = null; redirectTimes++; redirect = true; break; default: // Error: throw exception hcon.close(); throw new IOException("Response status not OK:" + status); } // max 5 redirects } while (redirect == true && redirectTimes < 5); if (redirectTimes == 5) { throw new IOException("Too much redirects"); } } catch (Exception e) { e.printStackTrace(); responseMessage.append("ERROR"); } finally { // free up i/o streams and http connection try { if (hcon != null) hcon.close(); if (dis != null) dis.close(); if (dos != null) dos.close(); } catch (IOException ioe) { ioe.printStackTrace(); } // end try/catch } // end try/catch/finally return responseMessage.toString(); }