📄 dec00_ericg.txt
字号:
To make an HTTP request, use the Connector.open method
and a conventional URL, casting the result to an HttpConnection:
import javax.microedition.io.*;
HttpConnection conn = Connector.open(
"http://www.java.sun.com/jdc" );
The HttpConnection interface defines all the usual methods you
would expect to see for making HTTP requests and processing the
replies. This includes setRequestMethod, getHeaderField and
openInputStream. The HttpConnection interface makes it simple
to interact with any web site on the Internet.
One thing you will soon discover is that you won't always get
the response you expect when you connect to a web site. In
particular, many web sites redirect you to another URL. The
Java Developer Connection web site does this, for example, when
you attempt to open the page at http://www.java.sun.com/jdc -- it
redirects you to http://developer.java.sun.com/developer/.
Sometimes a site redirects you because the resource you are
asking for has moved. Or else the site wants you to login first.
If you are accessing a web site that's not under your control,
you have to be prepared to handle redirections. You need to
follow the redirections to obtain the data you really want.
Here's a simple class called HttpConnectionHelper that
uses the HttpConnection interface. You can use the class to
automatically follow web site redirections:
import java.io.*;
import javax.microedition.io.*;
public class HttpConnectionHelper {
public interface Callback {
void prepareRequest( String originalURL,
HttpConnection conn )
throws IOException;
}
public static HttpConnection connect( String url )
throws IOException {
return connect( url, null );
}
public static HttpConnection connect(
String url, Callback callback )
throws IOException {
HttpConnection conn = null;
String originalURL = url;
while( url != null ){
HttpConnection conn = (HttpConnection)
Connector.open( url );
if( callback != null ){
callback.prepareRequest( originalURL, conn );
}
int rc = conn.getResponseCode();
switch( rc ){
case HttpConnection.HTTP_MOVED_PERM:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_SEE_OTHER:
case HttpConnection.HTTP_TEMP_REDIRECT:
url = conn.getHeaderField( "Location" );
if( url != null && url.startsWith(
"/" ) ){
StringBuffer b = new StringBuffer();
b.append( "http://" );
b.append( conn.getHost() );
b.append( ':' );
b.append( conn.getPort() );
b.append( url );
url = b.toString();
}
conn.close();
break;
default:
url = null;
break;
}
}
return conn;
}
}
To use this class, call the connect method, passing it the
original URL:
HttpConnection conn = HttpConnectionHelper.connect(
"http://java.sun.com/jdc" );
int rc = conn.getResponseCode();
if( rc == HttpConnection.HTTP_OK ){
InputStream in = conn.openInputStream();
// do something with the response....
} else {
// unexpected response code
}
When invoked, the connect method connects to the original URL
and gets the HTTP response code. If the response code is the
redirection code HTTP_TEMP_REDIRECT, the connect method gets the
new URL from the Location response header and connects to that
URL. The method loops until a non-redirection response code is
returned. At that point, the method returns an HttpConnection to
the final destination.
For special cases, such as when you want to make a Post request
instead of a Get request, you can use the two-parameter form of
the connect method. Here you pass in the URL as the first
parameter, and an object implementing the
HttpConnectionHelper.Callback interface as the second parameter:
class MyCallback implements HttpConnectionHelper.Callback {
public void prepareRequest( String originalURL,
HttpConnection conn )
throws IOException {
conn.setRequestMethod( HttpConnection.POST );
conn.setRequestProperty( "User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0" );
conn.setRequestProperty(
"Content-Language", "en-US" );
}
}
HttpConnection conn = HttpConnection.connect(
"http://java.sun.com/jdc", new MyCallback() );
If you define a callback, it's called each time a connection is
made, that is, before the response code is obtained. This lets you
modify the HTTP request before it's actually made.
For further information about the Mobile Information Device
Profile (MIDP), see http://java.sun.com/products/midp.
For more information about the HTTP 1.1 protocol, refer to
ftp://ftp.isi.edu/in-notes/rfc2616.txt.
. . . . . . . . . . . . . . . . . . . . . . .
- NOTE
Sun respects your online time and privacy. The Java Developer
Connection mailing lists are used for internal Sun Microsystems(tm)
purposes only. You have received this email because you elected
to subscribe. To unsubscribe, go to the Subscriptions page
(http://developer.java.sun.com/subscription/), uncheck the
appropriate checkbox, and click the Update button.
- SUBSCRIBE
To subscribe to a JDC newsletter mailing list, go to the
Subscriptions page (http://developer.java.sun.com/subscription/),
choose the newsletters you want to subscribe to, and click Update.
- FEEDBACK
Comments? Send your feedback on the J2ME Tech Tips to:
jdc-webmaster@sun.com
- ARCHIVES
You'll find the J2ME Tech Tips archives at:
http://java.sun.com/jdc/J2METechTips/index.html
- COPYRIGHT
Copyright 2000 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This document is protected by copyright. For more information, see:
http://java.sun.com/jdc/copyright.html
J2ME Tech Tips
December 18, 2000
* As used in this document, the terms "Java virtual machine"
or "JVM" mean a virtual machine for the Java platform.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -