在J2ME程序中,可以利用HttpConnection接口建立HTTP连接,访问远程服务器上的资源。具体的代码如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class GetHttpConnection extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public GetHttpConnection()
{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Get Http Connection");
//props.append("Hello World!\n");
try
{
HttpConnection conn=(HttpConnection) Connector.open
("
http://rainbow:8080/index.html" ;);
//conn.setRequestProperty("user","fancy");
props.append("Date:"+conn.getDate()+"\n");
props.append("Expiration:"+
conn.getExpiration()+"\n");
props.append(conn.getHost()+"\n");
props.append("Last Modified:"+
conn.getLastModified()+"\n");
props.append("Port:"+conn.getPort()+"\n");
props.append("Protocol:"+
conn.getProtocol()+"\n");
props.append("Request Method:"+
conn.getRequestMethod()+"\n");
props.append("Response Code:"+
conn.getResponseCode()+"\n");
props.append("Encoding:"+
conn.getEncoding()+"\n");
props.append("Length:"+conn.getLength()+"\n");
props.append("Type:"+conn.getType()+"\n");
props.append("URL:"+conn.getURL()+"\n");
props.append("Response Message:"+
conn.getResponseMessage()+"\n");
}
catch(Exception fe)
{
props.append("Error:"+fe.getMessage());
}
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
}
}
Connector类提供了open()方法,可以和各种各样的远程资源建立连接。open()方法的参数就是远程资源的URL地址。open()方法的返回值是一个Connection接口。为了建立HTTP连接,我们应该将它强制转换为HttpConnection接口的形式。一旦获取HttpConnection接
口的实例对象,就可以调用HttpConnection接口的各种方法,得到关于HTTP连接的各种信息。
我的想法是建立一个Web服务器,使用Tomcat 4.0,支持JSP技术。再配置一个Mail服务器,使用的软件是Imail 7.0.4。首先编写一个可以发送电子邮件的JSP程序(post.jsp),
具体代码
如下所示:
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%!
public class SmtpAuthenticator extends javax.mail.Authenticator
{
public javax.mail.PasswordAuthentication
getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("fancy",
"fancy");
}
}
%>
<%
String username="fancyrainbow";
String password="fancy";
String from="[email=fancy@rainbow]fancy@rainbow[/email]";
String to="[email=fancy@rainbow]fancy@rainbow[/email]";
String cc="[email=fancy@rainbow]fancy@rainbow[/email]";
String subject="J2ME Mail Test";
//String content="J2ME Mail Test";
String content=request.getParameter("content");
Properties props = System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","rainbow"); //263
SmtpAuthenticator sa=new SmtpAuthenticator();
Session sess = Session.getInstance(props, sa);
sess.setDebug(true);
Message msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,
false));
msg.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc,
false));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(content);
Transport.send(msg);
%>
Send Message OK!
我将post.jsp程序保存在Tomcat 4.0的ROOT目录下面,然后使用Web浏览器测试此程序成功。然后再将上一个J2ME程序(GetHttpConnection.java)改一改,让它与Tomcat 4.0
服务器建立连接,请求post.jsp程序。相关代码如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class SendMail extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public SendMail()
{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Get Http Connection");
//props.append("Hello World!\n");
try
{
HttpConnection
conn=(HttpConnection)Connector.open("
http://rainbow:8080/post.
jsp?content=Hello World");
//conn.setRequestProperty("user","fancy");
props.append("Date:"+conn.getDate()+"\n");
props.append("Expiration:"+conn.getExpiration()+"\n");
props.append(conn.getHost()+"\n");
props.append("Last Modified:"+conn.getLastModified()+"\n
props.append("Port:"+conn.getPort()+"\n");
props.append("Protocol:"+conn.getProtocol()+"\n");
props.append("Request Method:"+conn.getRequestMethod()+"
props.append("Response Code:"+conn.getResponseCode()+"\n
props.append("Encoding:"+conn.getEncoding()+"\n");
props.append("Length:"+conn.getLength()+"\n");
props.append("Type:"+conn.getType()+"\n");
props.append("URL:"+conn.getURL()+"\n");
props.append("Response
Message:"+conn.getResponseMessage()+"\n");
//InputStream is=conn.openInputStream();
//DataInputStream dis=new DataInputStream(is);
//props.append("System Info:"+dis.readUTF()+"\n");
props.append("Send Mail OK!"+"\n");
}
catch(Exception fe)
{
props.append("Error:"+fe.getMessage());
}
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
}
}
启动Tomcat 4.0、Imail 7.0.4、J2MEWTK。在J2MEWTK中运行SendMail.java程序
,
事后检查邮箱,发现已经成功的收到了邮件。
有两点需要说明一下:
首先,按照上面的模式,我们可以编写出功能更为强大的程序来,例如使用
J2ME+JDBC+JSP访问远程的数据库系统,访问EJB组件等等。
其次,如果要读取post.jsp程序的输出信息,你可以从HttpConnection接口中获取一个输入流对象,逐个读取输入流中的数据即可。具体的代码我就不举了,你可以参考J2ME
的DOC。 <!--v:3.2-->