How to call a SOAP webservice with a simple String (xml in string format)

前端 未结 3 2126
野趣味
野趣味 2021-01-06 07:28

I have this string representing a XML:

String soapCall=\"

        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 07:40

    Formatted answer :
    
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.methods.PostMethod;
    
    /**
     * * This program demonstrates consuming web service using Apache HTTPClient and
     * SOAP message. * Reference:
     * http://www.webservicex.net/stockquote.asmx?op=GetQuote * ClassPath: Keep these
     * two files in class path: commons-httpclient-3.0.1.jar, commons-codec-1.4.jar * @author
     * Bhavani P Polimetla * @since April-27-2011
     */
    public class WSClient {
        public static void main(String args[]) {
            HttpClient httpClient = new HttpClient();
            httpClient.getParams().setParameter("http.useragent",
                    "Web Service Test Client");
            BufferedReader br = null;
            String data = " "
                    + "   "
                    + "     "
                    + "       "
                    + "INFY     " + "   "
                    + " ";
            PostMethod methodPost = new PostMethod(
                    "http://www.webservicex.net/stockquote.asmx?op=GetQuote");
            methodPost.setRequestBody(data);
            methodPost.setRequestHeader("Content-Type", "text/xml");
            try {
                int returnCode = httpClient.executeMethod(methodPost);
                if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                    System.out
                            .println("The Post method is not implemented by this URI");
                    methodPost.getResponseBodyAsString();
                } else {
                    br = new BufferedReader(new InputStreamReader(
                            methodPost.getResponseBodyAsStream()));
                    String readLine;
                    while (((readLine = br.readLine()) != null)) {
                        System.out.println(readLine);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                methodPost.releaseConnection();
                if (br != null)             try {
                        br.close();
                    } catch (Exception fe) {
                        fe.printStackTrace();
                    }
            }
        }
    }
    

提交回复
热议问题