手動(dòng)SOAP Call WebService

          前不久公司接了個(gè)活,要求在Pnuts中Call WebService,本來是一件很容易的事,可不能添加第三方的類包給我造成了麻煩。不能使用Ajax等一些第三方的包,意味著我只能自己去解析SOAP協(xié)議,而在PNuts中雖然可以調(diào)JAVA的方法,但卻不適合寫很大的功能,最后采用了一種非常傻的做法,手動(dòng)去拼SOAP消息的內(nèi)容,寫在這作為對一些特殊需求的一種實(shí)現(xiàn)方式:

          try {
          ????postStr?
          = ?postStr? + ? " <?xml?version='1.0'?encoding='utf-8'?> " ;
          ????postStr?
          = ?postStr? + ? " <soap:Envelope?xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " ;
          ????postStr?
          = ?postStr? + ? " ?xmlns:xsd='http://www.w3.org/2001/XMLSchema' " ;
          ????postStr?
          = ?postStr? + ? " ?xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> " ;
          ????postStr?
          = ?postStr? + ? " <soap:Body> " ;
          ????postStr?
          = ?postStr? + ? " <exchangeData?xmlns='https://www.riverbed.com/mfg/DataExchange/'> " ;
          ????postStr?
          = ?postStr? + ? " <sender>Test</sender> " ;
          ????postStr?
          = ?postStr? + ? " <password>35b82957-c792-81ce-38e4-7c83ad3291ea</password> " ;
          ????postStr?
          = ?postStr? + ? " <transactionType>Test_Get_Data</transactionType> " ;
          ????postStr?
          = ?postStr? + ? " <data> " ;
          ????postStr?
          = ?postStr? + ?htmlEncode(data);
          ????postStr?
          = ?postStr? + ? " </data> " ;
          ????postStr?
          = ?postStr? + ? " </exchangeData> " ;
          ????postStr?
          = ?postStr? + ? " </soap:Body> " ;
          ????postStr?
          = ?postStr? + ? " </soap:Envelope> " ;

          ????URL?url?
          = ? new ?URL( " https://www.test.com/testdata.php " );
          ????HttpURLConnection?conn?
          = ?(HttpURLConnection)url.openConnection();
          ????conn.setDoOutput(
          true );
          ????conn.setRequestMethod(
          " POST " );
          ????conn.setRequestProperty(
          " Content-Type " , " text/xml " );
          ????conn.setRequestProperty(
          " Content-Length " ,?String.valueOf(postStr.length()));
          ????conn.setDoOutput(
          true );
          ????conn.setDoInput(
          true );
          ????conn.connect();
          ????PrintWriter?out?
          = ? new ?PrintWriter(conn.getOutputStream());??
          ????out.print(postStr);
          ????out.flush();
          ????out.close();
          ????BufferedReader?in?
          = ? new ?BufferedReader( new ???InputStreamReader(conn.getInputStream()));
          ????String?inputLine;
          ????
          while ((inputLine??? = ???in.readLine())??? != ??? null )? {
          ????????result???
          += ???inputLine + " \n " ;??
          ????}

          ????in.close();
          ????System.out.println(htmlDecode(result));
          }
          catch (Exception?ex) {
          ????System.out.println(
          " Error: " ? + ?ex.getMessage());
          }

          posted on 2006-12-19 09:40 EdwinWang 閱讀(1016) 評論(0)  編輯  收藏 所屬分類: JAVA相關(guān)


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2006年12月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 赣榆县| 宝应县| 桂林市| 图木舒克市| 汉川市| 赤城县| 漳平市| 大渡口区| 吕梁市| 饶河县| 英山县| 郑州市| 镇雄县| 凤翔县| 无极县| 温州市| 汉川市| 慈溪市| 塔河县| 唐山市| 腾冲县| 兰西县| 曲松县| 临澧县| 塔河县| 本溪| 翁源县| 瓦房店市| 阿瓦提县| 会理县| 方山县| 栾川县| 米林县| 福清市| 惠来县| 成安县| 灌云县| 永康市| 蛟河市| 湘阴县| 五莲县|