📄 httpconnection.html
字号:
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, 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; } process(data); } 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(); } } </pre> <hr> <p> <STRONG>Simplified Stream Methods on Connector</STRONG> <p> Please note the following: The <code>Connector</code> class defines the following convenience methods for retrieving an input or output stream directly for a specified URL: <UL> <LI> <CODE> InputStream openInputStream(String url) </CODE> <LI> <CODE> DataInputStream openDataInputStream(String url) </CODE> <LI> <CODE> OutputStream openOutputStream(String url) </CODE> <LI> <CODE> DataOutputStream openDataOutputStream(String url) </CODE> </UL> Please be aware that using these methods implies certain restrictions. You will not get a reference to the actual connection, but rather just references to the input or output stream of the connection. Not having a reference to the connection means that you will not be able to manipulate or query the connection directly. This in turn means that you will not be able to call any of the following methods: <UL> <LI> <CODE> getRequestMethod() </CODE> <LI> <CODE> setRequestMethod() </CODE> <LI> <CODE> getRequestProperty() </CODE> <LI> <CODE> setRequestProperty() </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>
<DL>
<DT><B>Since: </B><DD>MIDP 1.0</DD>
</DL>
<HR>
<P>
<!-- ======== INNER CLASS SUMMARY ======== -->
<!-- =========== FIELD SUMMARY =========== -->
<A NAME="field_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Field Summary</B></FONT></TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static <A HREF="../../../java/lang/String.html">String</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#GET">GET</A></B></CODE>
<BR>
HTTP Get method.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static <A HREF="../../../java/lang/String.html">String</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HEAD">HEAD</A></B></CODE>
<BR>
HTTP Head method.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_ACCEPTED">HTTP_ACCEPTED</A></B></CODE>
<BR>
202: The request has been accepted for processing, but the processing has not been completed.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_BAD_GATEWAY">HTTP_BAD_GATEWAY</A></B></CODE>
<BR>
502: The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_BAD_METHOD">HTTP_BAD_METHOD</A></B></CODE>
<BR>
405: The method specified in the Request-Line is not allowed for the resource identified by the Request-URI.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_BAD_REQUEST">HTTP_BAD_REQUEST</A></B></CODE>
<BR>
400: The request could not be understood by the server due to malformed syntax.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_CLIENT_TIMEOUT">HTTP_CLIENT_TIMEOUT</A></B></CODE>
<BR>
408: The client did not produce a request within the time that the server was prepared to wait.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_CONFLICT">HTTP_CONFLICT</A></B></CODE>
<BR>
409: The request could not be completed due to a conflict with the current state of the resource.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_CREATED">HTTP_CREATED</A></B></CODE>
<BR>
201: The request has been fulfilled and resulted in a new resource being created.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_ENTITY_TOO_LARGE">HTTP_ENTITY_TOO_LARGE</A></B></CODE>
<BR>
413: The server is refusing to process a request because the request entity is larger than the server is willing or able to process.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_EXPECT_FAILED">HTTP_EXPECT_FAILED</A></B></CODE>
<BR>
417: The expectation given in an Expect request-header field could not be met by this server, or, if the server is a proxy, the server has unambiguous evidence that the request could not be met by the next-hop server.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_FORBIDDEN">HTTP_FORBIDDEN</A></B></CODE>
<BR>
403: The server understood the request, but is refusing to fulfill it.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_GATEWAY_TIMEOUT">HTTP_GATEWAY_TIMEOUT</A></B></CODE>
<BR>
504: The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI or some other auxiliary server it needed to access in attempting to complete the request.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_GONE">HTTP_GONE</A></B></CODE>
<BR>
410: The requested resource is no longer available at the server and no forwarding address is known.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_INTERNAL_ERROR">HTTP_INTERNAL_ERROR</A></B></CODE>
<BR>
500: The server encountered an unexpected condition which prevented it from fulfilling the request.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_LENGTH_REQUIRED">HTTP_LENGTH_REQUIRED</A></B></CODE>
<BR>
411: The server refuses to accept the request without a defined Content- Length.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_MOVED_PERM">HTTP_MOVED_PERM</A></B></CODE>
<BR>
301: The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_MOVED_TEMP">HTTP_MOVED_TEMP</A></B></CODE>
<BR>
302: The requested resource resides temporarily under a different URI.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_MULT_CHOICE">HTTP_MULT_CHOICE</A></B></CODE>
<BR>
300: The requested resource corresponds to any one of a set of representations, each with its own specific location, and agent- driven negotiation information is being provided so that the user (or user agent) can select a preferred representation and redirect its request to that location.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_NO_CONTENT">HTTP_NO_CONTENT</A></B></CODE>
<BR>
204: The server has fulfilled the request but does not need to return an entity-body, and might want to return updated meta-information.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_NOT_ACCEPTABLE">HTTP_NOT_ACCEPTABLE</A></B></CODE>
<BR>
406: The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_NOT_AUTHORITATIVE">HTTP_NOT_AUTHORITATIVE</A></B></CODE>
<BR>
203: The returned meta-information in the entity-header is not the definitive set as available from the origin server.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../javax/microedition/io/HttpConnection.html#HTTP_NOT_FOUND">HTTP_NOT_FOUND</A></B></CODE>
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -