09_HTTPCLIETN Testing the SOAP protocol

Source: Internet
Author: User
Tags soap

"Engineering" NOTE: No need to generate client code using Wsimport

"Httpclient.java"

ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classHttpClient { Public Static voidMain (string[] args)throwsIOException {//Open an HTTP link//WebService AddressURL url =NewURL ("Http://127.0.0.1:12345/weather"); HttpURLConnection HttpURLConnection=(HttpURLConnection) url.openconnection (); //set POST request, post is uppercaseHttpurlconnection.setrequestmethod ("POST"); //content-type:text/xml; Charset=utf-8Httpurlconnection.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 "); //set up requests and responsesHttpurlconnection.setdoinput (true); Httpurlconnection.setdooutput (true); String requeststring= RequestString ("Zhengzhou"); //Send SOAP protocolHttpurlconnection.getoutputstream (). Write (Requeststring.getbytes ()); //Receive response contentInputStream InputStream=Httpurlconnection.getinputstream (); Bytearrayoutputstream Bytearrayoutputstream=NewBytearrayoutputstream (); intLen=-1; byte[] B =New byte[1024]; //Write InputStream content to Bytearrayoutputstream         while((len= inputstream.read (b, 0, 1024))!=-1) {Bytearrayoutputstream.write (b,0, Len); }                //Get Response ContentString responsestring =bytearrayoutputstream.tostring ();                System.out.println (responsestring); //parse the XML data for the response. //....Inputstream.close ();    Bytearrayoutputstream.close (); }        /**<?xml version= "1.0"?> <s:envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:queryweather xmlns:ns2="http://server.weather.jaxws.Higgin.com/"> <arg0> zhengzhou </arg0> </ns2:queryWeather> </S:BODY&G    T </S:Envelope>*/    //Content of the SOAP protocol, content of the request    Private Staticstring requeststring (String cityname) {string xmlstring="<?xml version=\" 1.0\ "?>" + "<s:envelope xmlns:s=\" http://schemas.xmlsoap.org/soap/envelope/\ ">" + "<S:Body>" + "<ns2:queryweather xmlns:ns2=\" http://server.weather.jaxws.Hi Ggin.com/\ ">" + "<arg0>" +cityname+ "</arg0>" + "</ns2:queryweather& gt; "+" </S:Body> "+" </S:Envelope> "; returnxmlstring; }}

"Run Results"

(Note: To start the WebService service first)

(need to further parse out the data you need, using regular expressions)

09_HTTPCLIETN Testing the SOAP protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.