关于cmwap的代理

本文介绍了在J2ME网络编程中如何使用CMWAP代理进行网络连接,提供了设置HttpConnection的代码示例,强调了设置"X-Online-Host"属性的重要性,并说明了实际应用中只需替换特定域名和URL即可实现代理连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在中国移动提供的网络连接中,分为 CMNETCMWAP两种,其中 CMNET可以无限制的访问互联网络,资费比较贵。 CMWAP类似一个 HTTP的代码,只能访问支持 HTTP的应用,但是资费便宜,稳定性比较差。

       在实际的J2ME网络编程中,一般需要提供以CMWAP代理的方式连接网络,在J2ME中,连接的代码和直接连接有所不同,代码如下:

              HttpConnection http = (HttpConnection)Connector.open(("http://10.0.0.172/"+url);

              http.setRequestProperty("X-Online-Host",ServerName);

       例如你需要访问的地址为:

              http://www.test.com/login/loginServlet

       则上面的代码就为:

              HttpConnection http = (HttpConnection)Connector.open((http://10.0.0.172/+”login/loginServlet”);

              http.setRequestProperty("X-Online-Host",”www.test.com”);

        在实际使用过程中,只需要使用实际需要访问的地址的域名或者IP来代替ServerName,例如示例中的“www.test.com”,使用后续的地址类代替代码中的url,例如示例中的“login/loginServlet”,就可以实际的使用CMWAP代理来进行连接了。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在反编译了数个J2ME游戏以及UCSDK之后,发现Http请求都使用到了cmwap代理,而代码几乎同上。

J2ME发起请求,并且获得返回数据:

    /**
     * 
     * @param host          IP + port 如 125.69.106.132:8099 (不要加http://)
     * @param url           /sk/payServlet (以/开头)
     * @param cmwap         是否使用cmwap接入(是否开启代理)
     * @throws IOException 
     */
    private static void URLRequest(String host, String url, boolean cmwap) throws IOException {
        HttpConnection c = null;
        InputStream is = null;
        int rc;

        try {
            if (cmwap) {
                c = (HttpConnection) Connector.open("http://10.0.0.172:80" + url, 3, true);
                c.setRequestProperty("X-Online-Host", host);
                c.setRequestProperty("Accept", "*/*");
            } else {
                c = (HttpConnection) Connector.open("http://" + host + url, 3, true);
            }
            
            OutputStream openOutputStream = c.openOutputStream();
            openOutputStream.write("Vicky".getBytes());
            openOutputStream.flush();
            openOutputStream.close();
            
            rc = c.getResponseCode();
            if (rc != HttpConnection.HTTP_OK) {
                throw new IOException("HTTP response code: " + rc);
            }

            is = c.openInputStream();

            String type = c.getType();

            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;
                }
                String msg = new String(data,"UTF-8");
                System.out.println("MSG>>>>>>>>>>>" + msg);
            }
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Not an HTTP URL");
        } finally {
            if (is != null) {
                is.close();
            }
            if (c != null) {
                c.close();
            }
        }
    }


Servlet获得请求处理,并返回数据。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        byte[] bufer = new byte[256];
        ServletInputStream inputStream = request.getInputStream();
        inputStream.read(bufer);
        inputStream.close();
        String getMsg = new String(bufer,"UTF-8");
        System.out.println("getMsg = " + getMsg);
        
        
        byte[] msg = "Hello World".getBytes();
        response.setContentLength(msg.length);
        response.setCharacterEncoding("UTF-8");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(msg);
        outputStream.flush();
        outputStream.close();
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值