2012-04-25 203 views
-1

如何執行soap web服務以及如何打印數據?如何在Java中創建soap請求和響應?

目前我使用下面的代碼

package com.appulento.pack; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class SimpleHTTPRequest 
{ 
    public static void main(String[] args) throws Exception { 
    final String url = 
     "http://**********:8000/sap/bc/srt/rfc/sap/zmaterials_details/" + 
      "800/zmaterials_details/zmaterials_details_bind", 
     soapAction ="urn:sap-com:document:sap:soap:functions:mc-style/ZMATERIALS_DETAILS", 
     envelope1="<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
     "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" + 
      " xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">" + 
     "<soapenv:Header>"+ 
     "<soapenv:Body>"+ 
     "<urn:ZMATERIALS_DETAILS>"+ 
     "<Language>D</Language>"+ 
     "<MaterialGroup>00208</MaterialGroup>"+ 
     "</urn:ZMATERIALS_DETAILS>"+ 
     "</soap:Body>"+ 
     "</soap:Envelope>" ; 
    HttpURLConnection connection = null; 
    try { 
     final URL serverAddress = new URL("http://*********:8000/sap/bc/srt/wsdl/"+ 
      "srvc_14DAE9C8D79F1EE196F1FC6C6518A345/wsdl11/allinone/ws_policy/" + 
      "document?sap-client=800&sap-user=************&sap-password=****"); 
     connection = (HttpURLConnection)serverAddress.openConnection(); 
     connection.setRequestProperty("SOAPAction", soapAction); 
     connection.setRequestMethod("POST"); 
     connection.setDoOutput(true); 
     final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); 
     writer.append(envelope1); 
     writer.close(); 
     final BufferedReader rd = 
      new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     String line; 
     while ((line = rd.readLine()) != null) System.out.println(line); 
    } finally { connection.disconnect(); } 
    } 
} 

我想發送XML作爲輸入請求,我想在XML中顯示了。

+0

你的代碼看起來很棒。現在只需使用'POST'而不是'GET',然後將您的請求寫入輸出。 – 2012-04-25 10:47:10

+0

感謝您的回覆我如何處理回覆 – 2012-04-25 11:08:23

+0

據我所知,您已經在處理它 - 寫入System.out。 – 2012-04-25 11:09:38

回答

0

Iit可以像使用httpConnection和解析響應一樣發送HTTP請求。 但它已被其他人使用,使用wsimport工具與-keep選項。它將爲您生成用於使用SOAP發送請求的Java工件。

+0

「我的代碼」中是否有任何錯誤,請參閱一次,讓我知道,以便我可以轉移到其他方式 – 2012-04-25 12:38:26