📄 httpconnection.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc on Wed Sep 24 14:57:48 PDT 2003 -->
<TITLE>
MID Profile: Interface HttpConnection
</TITLE>
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<!-- ========== START OF NAVBAR ========== -->
<A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/HttpConnection.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
<strong>MID Profile</strong></EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../javax/microedition/io/DatagramConnection.html"><B>PREV CLASS</B></A>
<A HREF="../../../javax/microedition/io/HttpsConnection.html"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../index.html" TARGET="_top"><B>FRAMES</B></A>
<A HREF="HttpConnection.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY: INNER | <A HREF="#field_summary">FIELD</A> | CONSTR | <A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL: <A HREF="#field_detail">FIELD</A> | CONSTR | <A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<!-- =========== END OF NAVBAR =========== -->
<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
<FONT SIZE="-1">
javax.microedition.io</FONT>
<BR>
Interface HttpConnection</H2>
<DL>
<DT><B>All Superinterfaces:</B> <DD><A HREF="../../../javax/microedition/io/Connection.html">Connection</A>, <A HREF="../../../javax/microedition/io/ContentConnection.html">ContentConnection</A>, <A HREF="../../../javax/microedition/io/InputConnection.html">InputConnection</A>, <A HREF="../../../javax/microedition/io/OutputConnection.html">OutputConnection</A>, <A HREF="../../../javax/microedition/io/StreamConnection.html">StreamConnection</A></DD>
</DL>
<DL>
<DT><B>All Known Subinterfaces:</B> <DD><A HREF="../../../javax/microedition/io/HttpsConnection.html">HttpsConnection</A></DD>
</DL>
<HR>
<DL>
<DT>public interface <B>HttpConnection</B><DT>extends <A HREF="../../../javax/microedition/io/ContentConnection.html">ContentConnection</A></DL>
<P>
This interface defines the necessary methods and constants for an HTTP connection. <P> 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: <UL> <LI> Setup, in which the request parameters can be set <LI> Connected, in which request parameters have been sent and the response is expected <LI> Closed, the final state, in which the HTTP connection as been terminated </UL> The following methods may be invoked only in the Setup state: <UL> <LI> <CODE> setRequestMethod</CODE> <LI> <CODE> setRequestProperty</CODE> </UL> The transition from Setup to Connected is caused by any method that requires data to be sent to or received from the server. <p> The following methods cause the transition to the Connected state when the connection is in Setup state. <UL> <LI> <CODE> openInputStream</CODE> <LI> <CODE> openDataInputStream</CODE> <LI> <CODE> getLength</CODE> <LI> <CODE> getType</CODE> <LI> <CODE> getEncoding</CODE> <LI> <CODE> getHeaderField</CODE> <LI> <CODE> getResponseCode</CODE> <LI> <CODE> getResponseMessage</CODE> <LI> <CODE> getHeaderFieldInt</CODE> <LI> <CODE> getHeaderFieldDate</CODE> <LI> <CODE> getExpiration</CODE> <LI> <CODE> getDate</CODE> <LI> <CODE> getLastModified</CODE> <LI> <CODE> getHeaderField</CODE> <LI> <CODE> getHeaderFieldKey</CODE> </UL> <P> The following methods may be invoked while the connection is in Setup or Connected state. <UL> <LI> <CODE> close</CODE> <LI> <CODE> getRequestMethod</CODE> <LI> <CODE> getRequestProperty</CODE> <LI> <CODE> getURL</CODE> <LI> <CODE> getProtocol</CODE> <LI> <CODE> getHost</CODE> <LI> <CODE> getFile</CODE> <LI> <CODE> getRef</CODE> <LI> <CODE> getPort</CODE> <LI> <CODE> getQuery</CODE> </UL> <P> After an output stream has been opened by the <code>openOutputStream</code> or <code>openDataOutputStream</code> methods, attempts to change the request parameters via <code>setRequestMethod</code> or the <code>setRequestProperty</code> are ignored. Once the request parameters have been sent, these methods will throw an <code>IOException</code>. When an output stream is closed via the <code>OutputStream.close</code> or <code>DataOutputStream.close</code> methods, the connection enters the Connected state. When the output stream is flushed via the <code>OutputStream.flush</code> or <code>DataOutputStream.flush</code> methods, the request parameters MUST be sent along with any data written to the stream. </P> The transition to Closed state from any other state is caused by the <code>close</code> method and the closing all of the streams that were opened from the connection. <P> <STRONG>Example using StreamConnection</STRONG> <p> Simple read of a URL using <code>StreamConnection</code>. No HTTP specific behavior is needed or used. (<strong>Note:</strong> this example ignores all HTTP response headers and the HTTP response code. Since a proxy or server may have sent an error response page, an application can not distinquish which data is retreived in the <code>InputStream</code>.) <p> <code>Connector.open</code> is used to open URL and a <code>StreamConnection</code> is returned. From the <code>StreamConnection</code> the <code>InputStream</code> 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. <p> <pre> 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(); } } </pre> <p> <STRONG>Example using ContentConnection</STRONG> <p> Simple read of a URL using <code>ContentConnection</code>. No HTTP specific behavior is needed or used. <p> <code>Connector.open</code> is used to open url and a <code>ContentConnection</code> is returned. The <code>ContentConnection</code> may be able to provide the length. If the length is available, it is used to read the data in bulk. From the <code>ContentConnection</code> the <code>InputStream</code> 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. <p> <pre> void getViaContentConnection(String url) throws IOException { ContentConnection c = null; DataInputStream is = null; try { c = (ContentConnection)Connector.open(url); int len = (int)c.getLength(); dis = c.openDataInputStream(); if (len > 0) { byte[] data = new byte[len]; dis.readFully(data); } else { int ch; while ((ch = dis.read()) != -1) { ... } } } finally { if (dis != null) dis.close(); if (c != null) c.close(); } } </pre> <p> <STRONG>Example using HttpConnection</STRONG> <p> Read the HTTP headers and the data using <code>HttpConnection</code>. <p> <code>Connector.open</code> is used to open url and a <code>HttpConnection</code> 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 <code>HttpConnection</code> the <code>InputStream</code> 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. <p> <pre> void getViaHttpConnection(String url) throws IOException { HttpConnection c = null; InputStream is = null; int rc; try { c = (HttpConnection)Connector.open(url); // 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(); // 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; } } else { int ch; while ((ch = is.read()) != -1) { ... } } } catch (ClassCastException e) { throw new IllegalArgumentException("Not an HTTP URL"); } finally { if (is != null) is.close(); if (c != null) c.close(); } } </pre> <p> <STRONG>Example using POST with HttpConnection</STRONG> <p> Post a request with some headers and content to the server and process the headers and content. <p> <code>Connector.open</code> is used to open url and a <code>HttpConnection</code> 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 <code>HttpConnection</code> the <code>InputStream</code> 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. <p> <pre> void postViaHttpConnection(String url) 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");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -