30. januar 2003 - 09:41Der er
6 kommentarer og 2 løsninger
lukke forbíndelse i en midlet
Er det ikke muligt at lukke forbindelsen i en midlet? Jeg har sat HttpConnection til .close(), men linien er stadig åben, indtil jeg lukker midleten. Findes der en anden måde at lukke forbindelsen? QD::
quaid: Du får ikke lov at kalde nogle system specifikke ting, af sikkerhedsmæssige årsager.
Jeg har lige postet det som står i starten af HttpConnection API'en, der bruger de .close()
public interface HttpConnection extends ContentConnection This interface defines the necessary methods and constants for an HTTP connection.
HTTP is a request-response protocol in which the parameters of request must be set before the request is sent. The connection exists in one of three states:
Setup, in which the connection has not been made to the server. Connected, in which the connection has been made, request parameters have been sent and the response is expected. Closed, in which the connection has been closed and the methods will throw an IOException if called. The following methods may be invoked only in the Setup state: setRequestMethod setRequestProperty The transition from Setup to Connected is caused by any method that requires data to be sent to or received from the server. The following methods cause the transition to the Connected state
openInputStream openOutputStream openDataInputStream openDataOutputStream getLength getType getEncoding getHeaderField getResponseCode getResponseMessage getHeaderFieldInt getHeaderFieldDate getExpiration getDate getLastModified getHeaderField getHeaderFieldKey The following methods may be invoked while the connection is open.
close getRequestMethod getRequestProperty getURL getProtocol getHost getFile getRef getPort getQuery Example using StreamConnection
Simple read of a url using StreamConnection. No HTTP specific behavior is needed or used.
Connector.open is used to open url and a StreamConnection is returned. From the StreamConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream are closed.
void getViaStreamConnection(String url) throws IOException { StreamConnection c = null; InputStream s = null; try { c = (StreamConnection)Connector.open(url); s = c.openInputStream(); int ch; while ((ch = s.read()) != -1) { ... } } finally { if (s != null) s.close(); if (c != null) c.close(); } }
Example using ContentConnection
Simple read of a url using ContentConnection. No HTTP specific behavior is needed or used.
Connector.open is used to open url and a ContentConnection is returned. The ContentConnection may be able to provide the length. If the length is available, it is used to read the data in bulk. From the ContentConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream are closed.
void getViaContentConnection(String url) throws IOException { ContentConnection c = null; InputStream is = null; try { c = (ContentConnection)Connector.open(url); int len = (int)c.getLength(); if (len > 0) { is = c.openInputStream(); byte[] data = new byte[len]; int actual = is.read(data); ... } else { int ch; while ((ch = is.read()) != -1) { ... } } } finally { if (is != null) is.close(); if (c != null) c.close(); } }
Example using HttpConnection
Read the HTTP headers and the data using HttpConnection.
Connector.open is used to open url and a HttpConnection is returned. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the HttpConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream are closed.
void getViaHttpConnection(String url) throws IOException { HttpConnection c = null; InputStream is = null; try { c = (HttpConnection)Connector.open(url);
// Getting the InputStream will open the connection // and read the HTTP headers. They are stored until // requested. is = c.openInputStream();
// Get the ContentType String type = c.getType();
// Get the length and process the data int len = (int)c.getLength(); if (len > 0) { byte[] data = new byte[len]; int actual = is.read(data); ... } else { int ch; while ((ch = is.read()) != -1) { ... } } } finally { if (is != null) is.close(); if (c != null) c.close(); } }
Example using POST with HttpConnection
Post a request with some headers and content to the server and process the headers and content.
Connector.open is used to open url and a HttpConnection is returned. The request method is set to POST and request headers set. A simple command is written and flushed. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the HttpConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream is closed.
void postViaHttpConnection(String url) throws IOException { HttpConnection c = null; InputStream is = null; OutputStream os = null;
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-1.0 Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language", "en-US");
// Getting the output stream may flush the headers os = c.openOutputStream(); os.write("LIST games\n".getBytes()); os.flush(); // Optional, openInputStream will flush
// Opening the InputStream will open the connection // and read the HTTP headers. They are stored until // requested. 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) { byte[] data = new byte[len]; int actual = is.read(data); process(data); } else { int ch; while ((ch = is.read()) != -1) { process((byte)ch); } } } finally { if (is != null) is.close(); if (os != null) os.close(); if (c != null) c.close(); } }
For jeg har et lille prob. (Iziqio@hotmail.com) tak
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.