2016-09-29 6 views
0

xmlというWebServiceレスポンスを取得するメソッドを作成する必要があります。私はJavaクラスで作成する方法を知っていますが、問題はwebServiceからxmlという応答を得ています。WebServiceレスポンスからXMLとしてレスポンスを取得する方法は?

これらのWebサービスはソープベースです。

ありがとうございます。

+1

あなたがこれまでにしようとしているものを共有してください。私たちはあなたを助けてくれるでしょう。 – Veeram

+0

コメントありがとうございました。私はjavaのMozilla Posterのようなメソッドを作りたいと思っています。 –

答えて

0

私はちょうど私の問題を解決しました。 HttpURLConnectionが私を助けます。

次のコードブロックは、javaでMozilla Posterのxml応答を取得するためのポスターを作成する方法を示しています。

public static void main(String[] args) { 
    try { 
     String uri = "http://test.com/IntegratedServices/IntegratedServices.asmx?op=GetUserInfo"; 

     String postData = new XmlTest().xmlRequest("QWERTY10"); 

     URL url = new URL(uri); 

     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); // This is important. If you not set doOutput it is default value is false and throws java.net.ProtocolException: cannot write to a URLConnection exception 
     connection.setRequestMethod("POST"); // This is method type. If you are using GET method you can pass by url. If method post you must write 
     connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); // it is important if you post utf-8 characters 

     DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); // This three lines is importy for POST method. I wrote preceding comment. 
     wr.write(postData.getBytes()); 
     wr.close(); 

     InputStream xml = connection.getInputStream(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(xml)); 
     String line = ""; 
     String xmlResponse = ""; 
     while ((line = reader.readLine()) != null) { 
      xmlResponse += line; 
     } 

     File file = new File("D://test.xml"); // If you want to write as file to local. 
     FileWriter fileWriter = new FileWriter(file); 
     fileWriter.write(xmlResponse); 
     fileWriter.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public String xmlRequest(String pin) { 
    return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
      + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" 
      + " <soap:Body>\n" 
      + " <GetUserInfo xmlns=\"http://tempuri.org/\">\n" 
      + "  <pin>" + pin + "</pin>\n" 
      + " </GetUserInfo>\n" 
      + " </soap:Body>\n" 
      + "</soap:Envelope>"; 
} 

私は、これは、応答としてxml取得したい人に役立ちます願っています。また、私は自分のコードに詳細なコメントを書いた。 SOAP型WebServiceの

関連する問題