发新话题
打印

[J2ME] J2ME下访问.net的webservice

您是本帖第831个浏览者

J2ME下访问.net的webservice

添加的代码如下:

package org.ksoap2.transport;包

HttpTransport 类中的

public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException {

        if (soapAction == null)
            soapAction = "\"\"";

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XmlSerializer xw = new KXmlSerializer();
        xw.setOutput(bos, null);
        //2008-03-31 +
        xw.startDocument("UTF-8", java.lang.Boolean.FALSE);
        //---
        envelope.write(xw);
        xw.flush();
        bos.write('\r');
        bos.write('\n');
        byte[] requestData = bos.toByteArray();
        bos = null;
        xw = null;

        requestDump = debug ? new String(requestData) : null;
        responseDump = null;

        try {
            connected = true;
   
   /////////////////////////////////////////////
   /*
    * 下面的代码经修改是为了走cmwap代理连接互联网
    *
    * 举个例子:
    *  走cmwap代理:sRequestURL=http://10.0.0.172/flickrBuddy/services/Buddycast
     走cmwap代理:X-Online-Host=http://219.238.239.166:8080
    */
   // zhengyun added 20051230
   int nPosIndex = -1;
   String sServerIp = "";
   String sSuffix = "";     
   String sRequestURL = "";
   
   if (proxy)
   {
    if(url.startsWith("http://"))
    {
     nPosIndex = url.substring(7, url.length()).indexOf('/') + 7;
     sServerIp = url.substring(7, nPosIndex); // -> www.51myit.com
    }
    else
    {
     nPosIndex = url.indexOf('/');
     sServerIp = url.substring(0, nPosIndex); // -> www.51myit.com
    }
      
       sSuffix = url.substring(nPosIndex); // -> /bbs/post.asp...
   
    sRequestURL = CMCC_GATEWAY + sSuffix;
    System.out.println("走cmwap代理:sRequestURL=" + sRequestURL);
   }
   else{
    sRequestURL = url;
    System.out.println("走cmnet!");
      }
   
            connection =
                (HttpConnection) Connector.open(
     sRequestURL,
                    Connector.READ_WRITE,
                    true);
   
   // 注意X-Online-Host
   if (proxy) {
    connection.setRequestProperty("X-Online-Host", sServerIp);
    System.out.println("走cmwap代理:X-Online-Host=" + sServerIp);
             }
   /////////////////////////////////////////////////

            connection.setRequestProperty("SOAPAction", soapAction);
            connection.setRequestProperty("Content-Type", "text/xml");
            connection.setRequestProperty(
                "Content-Length",
                "" + requestData.length);

            connection.setRequestProperty("User-Agent", "Nokia kSOAP/2.0");
   connection.setRequestProperty("Accept", "*/*");

            connection.setRequestMethod(HttpConnection.POST);

            os = connection.openOutputStream();
            os.write(requestData, 0, requestData.length);
            //            os.flush ();  // removed in order to avoid chunked encoding
            os.close();
            System.out.println("连接上网络!");
            requestData = null;
      
            is = connection.openInputStream();

            if (debug) {
                bos = new ByteArrayOutputStream();
                byte[] buf = new byte[256];

                while (true) {
                    int rd = is.read(buf, 0, 256);
                    if (rd == -1)
                        break;
                    bos.write(buf, 0, rd);
                }

                buf = bos.toByteArray();
                responseDump = new String(buf);
                is.close();
                is = new ByteArrayInputStream(buf);
            }
           
            XmlPullParser xp = new KXmlParser();
            xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
            xp.setInput (is, null);
            System.out.println("开始解析!");
            envelope.parse(xp);
            //System.out.println("多大 ");
        }
        finally {
         System.out.println("淡淡的 "+connected);
            if (!connected)
                throw new InterruptedIOException();
            reset();
           
        }


  if(envelope.bodyIn instanceof SoapFault)
   throw ((SoapFault) envelope.bodyIn);

    }
不放弃!不抛弃!一个人的世界不完美!

TOP

好.
我的QQ:896932028

TOP

发新话题