javax.microedition.io.HttpConnection Java Examples
The following examples show how to use
javax.microedition.io.HttpConnection.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpdateTask.java From google-authenticator with Apache License 2.0 | 6 votes |
private static String getEncoding(HttpConnection c) throws IOException { String enc = "ISO-8859-1"; String contentType = c.getHeaderField("Content-Type"); if (contentType != null) { String prefix = "charset="; int beginIndex = contentType.indexOf(prefix); if (beginIndex != -1) { beginIndex += prefix.length(); int endIndex = contentType.indexOf(';', beginIndex); if (endIndex != -1) { enc = contentType.substring(beginIndex, endIndex); } else { enc = contentType.substring(beginIndex); } } } return enc.trim(); }
Example #2
Source File: HttpClientConnection.java From knopflerfish.org with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void setRequestProperty(String key, String value) throws IOException { if (state == STATE_CLOSED) { init(); } if (state != STATE_SETUP) { throw new IllegalStateException("Already connected"); } if (out != null && !(HttpConnection.POST.equals(method) || PUT_METHOD.equals(method))) { // When an outputstream has been created these calls are ignored. return ; } HttpMethod res = getResult(); res.setRequestHeader(key, value); }
Example #3
Source File: GameCanvasImplementation.java From CodenameOne with GNU General Public License v2.0 | 6 votes |
/** * @inheritDoc */ public String[] getHeaderFieldNames(Object connection) throws IOException { HttpConnection c = (HttpConnection)connection; Vector r = new Vector(); int i = 0; String key = c.getHeaderFieldKey(i); while (key != null) { if(r.indexOf(key) < 0) { r.addElement(key); } i++; key = c.getHeaderFieldKey(i); } if(r.size() == 0) { return null; } String[] response = new String[r.size()]; for(int iter = 0 ; iter < response.length ; iter++) { response[iter] = (String)r.elementAt(iter); } return response; }
Example #4
Source File: VideoCanvas.java From pluotsorbet with GNU General Public License v2.0 | 6 votes |
/** * Reads the content from the specified HTTP URL and returns InputStream * where the contents are read. * * @return InputStream * @throws IOException */ private InputStream urlToStream(String url) throws IOException { // Open connection to the http url... HttpConnection connection = (HttpConnection) Connector.open(url); DataInputStream dataIn = connection.openDataInputStream(); byte[] buffer = new byte[1000]; int read = -1; // Read the content from url. ByteArrayOutputStream byteout = new ByteArrayOutputStream(); while ((read = dataIn.read(buffer)) >= 0) { byteout.write(buffer, 0, read); } dataIn.close(); connection.close(); // Fill InputStream to return with content read from the URL. ByteArrayInputStream byteIn = new ByteArrayInputStream(byteout.toByteArray()); return byteIn; }
Example #5
Source File: Media.java From pluotsorbet with GNU General Public License v2.0 | 6 votes |
/** * Reads the content from the specified HTTP URL and returns InputStream * where the contents are read. * * @return InputStream * @throws IOException */ private InputStream urlToStream(String url) throws IOException { // Open connection to the http url... HttpConnection connection = (HttpConnection) Connector.open(url); DataInputStream dataIn = connection.openDataInputStream(); byte[] buffer = new byte[1000]; int read = -1; // Read the content from url. ByteArrayOutputStream byteout = new ByteArrayOutputStream(); while ((read = dataIn.read(buffer)) >= 0) { byteout.write(buffer, 0, read); } dataIn.close(); // Fill InputStream to return with content read from the URL. ByteArrayInputStream byteIn = new ByteArrayInputStream(byteout.toByteArray()); return byteIn; }
Example #6
Source File: BlackBerryImplementation.java From CodenameOne with GNU General Public License v2.0 | 6 votes |
/** * @inheritDoc */ public String[] getHeaderFields(String name, Object connection) throws IOException { HttpConnection c = (HttpConnection) connection; Vector r = new Vector(); int i = 0; while (c.getHeaderFieldKey(i) != null) { if (c.getHeaderFieldKey(i).equalsIgnoreCase(name)) { String val = c.getHeaderField(i); r.addElement(val); } i++; } if (r.size() == 0) { return null; } String[] response = new String[r.size()]; for (int iter = 0; iter < response.length; iter++) { response[iter] = (String) r.elementAt(iter); } return response; }
Example #7
Source File: BlackBerryImplementation.java From CodenameOne with GNU General Public License v2.0 | 6 votes |
/** * @inheritDoc */ public String[] getHeaderFieldNames(Object connection) throws IOException { HttpConnection c = (HttpConnection)connection; Vector r = new Vector(); int i = 0; String key = c.getHeaderFieldKey(i); while (key != null) { if(r.indexOf(key) < 0) { r.addElement(key); } i++; key = c.getHeaderFieldKey(i); } if(r.size() == 0) { return null; } String[] response = new String[r.size()]; for(int iter = 0 ; iter < response.length ; iter++) { response[iter] = (String)r.elementAt(iter); } return response; }
Example #8
Source File: ContentReader.java From pluotsorbet with GNU General Public License v2.0 | 5 votes |
/** * Finds the type of the content in this Invocation. * <p> * The calling thread blocks while the type is being determined. * If a network access is needed there may be an associated delay. * * @return the content type. * May be <code>null</code> if the type can not be determined. * * @exception IOException if access to the content fails * @exception IllegalArgumentException if the content is accessed via * the URL and the URL is invalid * @exception SecurityException is thrown if access to the content * is required and is not permitted */ String findType() throws IOException, SecurityException { String type = null; Connection conn = openPrim(true); if (conn instanceof ContentConnection) { if( conn instanceof HttpConnection ){ HttpConnection hc = (HttpConnection)conn; hc.setRequestMethod(HttpConnection.HEAD); // actual connection performed, some delay... if (hc.getResponseCode() != HttpConnection.HTTP_OK) return null; } type = ((ContentConnection)conn).getType(); conn.close(); if (type != null) { // Check for and remove any parameters (rfc2616) int ndx = type.indexOf(';'); if (ndx >= 0) { type = type.substring(0, ndx); } type = type.trim(); if (type.length() == 0) { type = null; } } } return type; }
Example #9
Source File: BlackBerryOS5Implementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * (non-Javadoc) * * @see * com.codename1.impl.blackberry.BlackBerryImplementation#openOutputStream(java.lang.Object) */ public OutputStream openOutputStream(Object connection) throws IOException { if (connection instanceof String) { return super.openOutputStream(connection); } OutputStream os = ((HttpConnection) connection).openOutputStream(); // getSoftwareVersion() not available in legacy port,introduced at API 4.3.0 int majorVersion = DeviceInfo.getSoftwareVersion().charAt(0) - '0'; // in version 7, BBOS started supporting HTTP 1.1, so facade not required. if (majorVersion < 7) { os = new BlackBerryOutputStream(os); } return new BufferedOutputStream(os, ((HttpConnection) connection).getURL()); }
Example #10
Source File: BlackBerryImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public void setHeader(Object connection, String key, String val) { try { ((HttpConnection) connection).setRequestProperty(key, val); } catch (IOException err) { // this exception doesn't make sense since at this point no connection is in place err.printStackTrace(); } }
Example #11
Source File: BlackBerryImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public OutputStream openOutputStream(Object connection) throws IOException { if (connection instanceof String) { FileConnection fc = (FileConnection) Connector.open((String) connection, Connector.READ_WRITE); if (!fc.exists()) { fc.create(); } BufferedOutputStream o = new BufferedOutputStream(fc.openOutputStream(), (String) connection); o.setConnection(fc); return o; } OutputStream os = new BlackBerryOutputStream(((HttpConnection) connection).openOutputStream()); return new BufferedOutputStream(os, ((HttpConnection) connection).getURL()); }
Example #12
Source File: BlackBerryImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public InputStream openInputStream(Object connection) throws IOException { if (connection instanceof String) { FileConnection fc = (FileConnection) Connector.open((String) connection, Connector.READ); BufferedInputStream o = new BufferedInputStream(fc.openInputStream(), (String) connection); o.setConnection(fc); return o; } return new BufferedInputStream(((HttpConnection) connection).openInputStream(), ((HttpConnection) connection).getURL()); }
Example #13
Source File: BlackBerryImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public void setPostRequest(Object connection, boolean p) { try { if (p) { ((HttpConnection) connection).setRequestMethod(HttpConnection.POST); } else { ((HttpConnection) connection).setRequestMethod(HttpConnection.GET); } } catch (IOException err) { // an exception here doesn't make sense err.printStackTrace(); } }
Example #14
Source File: Loader.java From J2ME-Loader with Apache License 2.0 | 5 votes |
/** * Open a HTTP stream and check its MIME type * * @param name Resource name * @return a http stream and checks the MIME type */ private InputStream getHttpInputStream(String name) throws IOException { InputConnection ic = (InputConnection) Connector.open(name); // Content-Type is available for http and https connections if (ic instanceof HttpConnection) { HttpConnection hc = (HttpConnection) ic; // Check MIME type String type = hc.getHeaderField("Content-Type"); if (type != null && !type.equals("application/m3g") && !type.equals("image/png") && !type.equals("image/jpeg")) { throw new IOException("Wrong MIME type: " + type + "."); } } InputStream is; try { is = ic.openInputStream(); } finally { try { ic.close(); } catch (Exception e) { e.printStackTrace(); } } return is; }
Example #15
Source File: HttpClient.java From pluotsorbet with GNU General Public License v2.0 | 5 votes |
/** * Read cookies from the HttpConnection and store them in the cookie jar. * * @param hcon HttpConnection object * @throws IOException */ private void readCookies(HttpConnection hcon) throws IOException { String headerName = hcon.getHeaderField(0); // Loop through the headers, filtering "Set-Cookie" headers for (int i = 0; headerName != null; i++) { headerName = hcon.getHeaderFieldKey(i); if (headerName != null && headerName.toLowerCase().equals("set-cookie")) { String cookieContent = hcon.getHeaderField(i); COOKIE_JAR.put(cookieContent); } } }
Example #16
Source File: HttpClient.java From pluotsorbet with GNU General Public License v2.0 | 5 votes |
/** * Take cookies from the cookie jar and write them as request propreties * in the HttpConnection. * * @param hcon HttpConnection object * @throws IOException */ private void writeCookies(HttpConnection hcon) throws IOException { String cookieHeader = COOKIE_JAR.getCookieHeader(); if (cookieHeader == null) { return; } hcon.setRequestProperty("Cookie", cookieHeader); }
Example #17
Source File: UpdateTask.java From google-authenticator with Apache License 2.0 | 5 votes |
private static HttpConnection connect(String url) throws IOException { if (DeviceInfo.isSimulator()) { url += ";deviceside=true"; } else { url += ";deviceside=false;ConnectionType=mds-public"; } return (HttpConnection) Connector.open(url); }
Example #18
Source File: Connection.java From J2ME-Loader with Apache License 2.0 | 5 votes |
@Override public void setRequestMethod(String method) throws IOException { if (cn == null) { throw new IOException(); } if (method.equals(HttpConnection.POST)) { cn.setDoOutput(true); } if (cn instanceof HttpURLConnection) { ((HttpURLConnection) cn).setRequestMethod(method); } }
Example #19
Source File: HttpClientConnection.java From knopflerfish.org with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void setRequestMethod(String method) throws IOException { if (!HttpConnection.GET.equals(method) && !HttpConnection.HEAD.equals(method) && !HttpConnection.POST.equals(method) && !PUT_METHOD.equals(method) && !DELETE_METHOD.equals(method)) { throw new IllegalArgumentException("method should be one of " + HttpConnection.GET + ", " + HttpConnection.HEAD + ", " + HttpConnection.POST + ", " + PUT_METHOD + " and " + DELETE_METHOD); } if (state == STATE_CLOSED) { init(); } if (state != STATE_SETUP) { throw new ProtocolException("Can't reset method: already connected"); } if (out != null && !(HttpConnection.POST.equals(method) || PUT_METHOD.equals(method))) { // When an outputstream has been created these calls are ignored. return ; } if (this.method != method && resCache != null) { // TODO: here we should convert an existing resCache throw new RuntimeException ("Not yet implemented, as a work around you can always set" +" the request method the first thing you do.."); } this.method = method; }
Example #20
Source File: HttpClientConnection.java From knopflerfish.org with BSD 3-Clause "New" or "Revised" License | 5 votes |
public OutputStream openOutputStream() throws IOException { if (requestSent) { throw new ProtocolException("The request has already been sent"); } if (out == null) { out = new OutputWrapper(); } if(!(HttpConnection.POST.equals(method) || PUT_METHOD.equals(method))) { setRequestMethod(HttpConnection.POST); } return out; }
Example #21
Source File: HttpClientConnection.java From knopflerfish.org with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void init() { state = STATE_SETUP; requestSent = false; method = HttpConnection.GET; resCache = null; //params = new DefaultHttpParams(); out = null; }
Example #22
Source File: GameCanvasImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public void setPostRequest(Object connection, boolean p) { try { if(p) { ((HttpConnection)connection).setRequestMethod(HttpConnection.POST); } else { ((HttpConnection)connection).setRequestMethod(HttpConnection.GET); } } catch(IOException err) { // an exception here doesn't make sense err.printStackTrace(); } }
Example #23
Source File: GameCanvasImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public void setHeader(Object connection, String key, String val) { try { ((HttpConnection)connection).setRequestProperty(key, val); } catch(IOException err) { // this exception doesn't make sense since at this point no connection is in place err.printStackTrace(); } }
Example #24
Source File: GameCanvasImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public InputStream openInputStream(Object connection) throws IOException { if(connection instanceof String) { FileConnection fc = (FileConnection)Connector.open((String)connection, Connector.READ); BufferedInputStream o = new BufferedInputStream(fc.openInputStream(), (String)connection); o.setConnection(fc); return o; } return new BufferedInputStream(((HttpConnection)connection).openInputStream(), ((HttpConnection)connection).getURL()); }
Example #25
Source File: GameCanvasImplementation.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * @inheritDoc */ public OutputStream openOutputStream(Object connection) throws IOException { if(connection instanceof String) { FileConnection fc = (FileConnection)Connector.open((String)connection, Connector.READ_WRITE); if(!fc.exists()) { fc.create(); } BufferedOutputStream o = new BufferedOutputStream(fc.openOutputStream(), (String)connection); o.setConnection(fc); return o; } return new BufferedOutputStream(((HttpConnection)connection).openOutputStream(), ((HttpConnection)connection).getURL()); }
Example #26
Source File: UpdateTask.java From google-authenticator with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public void run() { try { // Visit the original download URL and read the JAD; // if the MIDlet-Version has changed, invoke the callback. String url = Build.DOWNLOAD_URL; String applicationVersion = getApplicationVersion(); String userAgent = getUserAgent(); String language = getLanguage(); for (int redirectCount = 0; redirectCount < 10; redirectCount++) { HttpConnection c = null; InputStream s = null; try { c = connect(url); c.setRequestMethod(HttpConnection.GET); c.setRequestProperty("User-Agent", userAgent); c.setRequestProperty("Accept-Language", language); int responseCode = c.getResponseCode(); if (responseCode == HttpConnection.HTTP_MOVED_PERM || responseCode == HttpConnection.HTTP_MOVED_TEMP) { String location = c.getHeaderField("Location"); if (location != null) { url = location; continue; } else { throw new IOException("Location header missing"); } } else if (responseCode != HttpConnection.HTTP_OK) { throw new IOException("Unexpected response code: " + responseCode); } s = c.openInputStream(); String enc = getEncoding(c); Reader reader = new InputStreamReader(s, enc); final String version = getMIDletVersion(reader); if (version == null) { throw new IOException("MIDlet-Version not found"); } else if (!version.equals(applicationVersion)) { Application application = Application.getApplication(); application.invokeLater(new Runnable() { public void run() { mCallback.onUpdate(version); } }); } else { // Already running latest version } } finally { if (s != null) { s.close(); } if (c != null) { c.close(); } } } } catch (Exception e) { System.out.println(e); } }
Example #27
Source File: GameCanvasImplementation.java From CodenameOne with GNU General Public License v2.0 | 4 votes |
/** * @inheritDoc */ public String[] getHeaderFields(String name, Object connection) throws IOException { HttpConnection c = (HttpConnection)connection; Vector r = new Vector(); int i = 0; while (c.getHeaderFieldKey(i) != null) { if (c.getHeaderFieldKey(i).equalsIgnoreCase(name)) { String val = c.getHeaderField(i); //some J2ME devices such as Nokia send all the cookies in one //header spereated by commas if(name.equalsIgnoreCase("Set-Cookie")){ //it is not possible to simply tokenize on comma, because //the expiration date of each cookie contains a comma, therefore //we temporary remove this comma tokenize all the cookies and then //fixing the comma in the expiration date String cookies = ""; Vector v = StringUtil.tokenizeString(val, ';'); for (int j = 0; j < v.size(); j++) { String keyval = (String) v.elementAt(j); if(keyval.indexOf("expires") > -1){ keyval = StringUtil.replaceAll(keyval, ",", "@__@"); } cookies += keyval + ";"; } cookies = cookies.substring(0, cookies.length() - 1); if(cookies.indexOf(",") > -1){ Vector v2 = StringUtil.tokenizeString(cookies, ','); for (int j = 0; j < v2.size(); j++) { String value = (String) v2.elementAt(j); value = StringUtil.replaceAll(value, "@__@", ","); r.addElement(value); } } }else{ r.addElement(val); } } i++; } if(r.size() == 0) { return null; } String[] response = new String[r.size()]; for(int iter = 0 ; iter < response.length ; iter++) { response[iter] = (String)r.elementAt(iter); } return response; }
Example #28
Source File: VotePostOperation.java From pluotsorbet with GNU General Public License v2.0 | 4 votes |
public String getRequestMethod() { return HttpConnection.POST; }
Example #29
Source File: CommentPostOperation.java From pluotsorbet with GNU General Public License v2.0 | 4 votes |
public String getRequestMethod() { return HttpConnection.POST; }
Example #30
Source File: LoginOperation.java From pluotsorbet with GNU General Public License v2.0 | 4 votes |
/** * This operation uses the HTTP POST method. */ public String getRequestMethod() { return HttpConnection.POST; }