Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 380|回复: 0

[J2ME学习]基于HTTP向服务器发POST请求

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-10 23:49:49 | 显示全部楼层 |阅读模式
    1.下面的Servlet程序读取手机程序发送过来的两个参数信息,一个是用户账号,另一个是密码,然后将这两个信息返回到
       
    手机程序。
       

       

       
       

       
    package com.Test;
       
    import java.io.*;
       
    import javax.servlet.*;
       
    import javax.servlet.http.*;
       
      
       
       
         
       

       
       
      
      

    1. public class HelloWorld extends HttpServlet {
    2.     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    3.         // Read the parameters sent from MIDlet
    4.        String acct = req.getParameter("account"), pwd = req
    5.                 .getParameter("password");
    6.         if (acct == null || pwd == null) {
    7.             res.sendError(HttpServletResponse.SC_BAD_REQUEST,
    8.                     "Unable to read parameters");
    9.             return;
    10.         }
    11.         res.setContentType("text/plain");
    12.         PrintWriter out = res.getWriter();
    13.         out.print(this.getServletInfo() + "
    14. ");
    15.         out.print("acct:" + acct + "
    16. pwd:" + pwd);
    17.         out.close();
    18.     }
    19.     protected void doPost(HttpServletRequest req, HttpServletResponse res)
    20.             throws ServletException, IOException {
    21.         doGet(req, res);
    22.     }
    23.     /*--------------------------------------------------
    24.      * Information about servlet
    25.      *-------------------------------------------------*/
    26.     public String getServletInfo() {
    27.         return "Hello,World

    28. ";
    29.     }
    30. }
    31. 2、下面的J2ME程序通过HTTP连接方式访问servlet,并发送POST的请求,然后将servlet程序的响应信息通过一个Form显示在手机屏幕上。
    32. /*
    33. * 使用POST方式访问Servlet
    34. */
    35. import javax.microedition.midlet.*;
    36. import javax.microedition.lcdui.*;
    37. import javax.microedition.io.*;
    38. import java.io.*;
    39. public class HttpPostExample extends MIDlet implements CommandListener {
    40.         private Display display;
    41.         private Form mainForm;
    42.         private Command exitCommand;
    43.         String account = "newuser";
    44.         String password = "123456";
    45.         public HttpPostExample() {
    46.                 display = Display.getDisplay(this);
    47.                 exitCommand = new Command("Exit", Command.EXIT, 1);
    48.                 mainForm = new Form("服务器的信息");
    49.                 mainForm.addCommand(exitCommand);
    50.                 mainForm.setCommandListener(this);
    51.         }
    52.         public void startApp() {
    53.                 display.setCurrent(mainForm);
    54.                 //访问服务器servlet
    55.                 try {
    56.                         postServlet();
    57.                 } catch (Exception e) {
    58.                         System.out.println(e.toString());
    59.                 }
    60.         }
    61.         public void pauseApp() {
    62.         }
    63.         public void destroyApp(boolean unconditional) {
    64.         }
    65.         public void postServlet() throws IOException {
    66.                 HttpConnection http = null;
    67.                 InputStream iStrm = null;
    68.                 OutputStream oStrm = null;
    69.                 // Data is passed at the end of url for Post
    70.                 String url = "http://127.0.0.1:8080/examples/hello";
    71.                 String rawData = "account=long&password=heihei";               
    72.                
    73.                 try {
    74.                         http = (HttpConnection) Connector.open(url);
    75.                         http.setRequestMethod(HttpConnection.POST);
    76.                         http.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
    77.                         http.setRequestProperty("Content-Language", "en-US");
    78.             http.setRequestProperty("Content-Length",String.valueOf(rawData.length()));//设置参数长度
    79.                         http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    80.                                        
    81.                        
    82.                         oStrm =http.openOutputStream();
    83.                         oStrm.write(rawData.getBytes());
    84.                         //oStrm.flush();
    85.                         if (http.getResponseCode() == HttpConnection.HTTP_OK) {
    86.                                 iStrm = http.openInputStream();
    87.                                 // 获得头信息,没有返回
    88.                                 //获得数据信息
    89.                                 int length = (int) http.getLength();
    90.                                 if (length > 0) {
    91.                                         byte servletData[] = new byte[length];
    92.                                         iStrm.read(servletData);
    93.                                         //显示返回信息
    94.                                         mainForm.append("使用Post方式验证通过:
    95. " + new String(servletData));
    96.                                 } else
    97.                                         mainForm.append("不能访问数据!");
    98.                         }
    99.                 } catch (Exception e) {
    100.                         mainForm.append("网络出错");
    101.                         System.out.println(e.toString());
    102.                 } finally {
    103.                         //关闭连接对象
    104.                         if (iStrm != null)
    105.                                 iStrm.close();
    106.                         if (http != null)
    107.                                 http.close();
    108.                         if (oStrm != null)
    109.                                 oStrm.close();
    110.                        
    111.                 }
    112.         }
    113.         public void commandAction(Command c, Displayable s) {
    114.                 if (c == exitCommand) {
    115.                         destroyApp(false);
    116.                         notifyDestroyed();
    117.                 }
    118.         }
    119. }                    
    复制代码

       
         
         
          
          

            
          

            
          
         
       

    1.                         
    复制代码

      


    源码下载:http://203.93.208.26/kj/cwb/dir7/http1.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-22 22:19 , Processed in 0.380915 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表