手动SOAP Call WebService

前不久公司接了个活,要求在Pnuts中Call WebService,本来是一件很容易的事,可不能添加第三方的类包给我造成了麻烦。不能使用Ajax等一些第三方的包,意味着我只能自己去解析SOAP协议,而在PNuts中虽然可以调JAVA的方法,但却不适合写很大的功能,最后采用了一种非常傻的做法,手动去拼SOAP消息的内容,写在这作为对一些特殊需求的一种实现方式:

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 阅读(1002) 评论(0)  编辑  收藏 所属分类: JAVA相关


只有注册用户登录后才能发表评论。


网站导航:
 
<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜