"WebService" Debug method article (two, Java HTTP request) __web

Source: Internet
Author: User

WebService debugging Tools mentioned earlier, if you are interested in writing test code, it is very good ah.

I also think so, there are ideas may wish to try Oh. Then, my program mainly uses the HttpURLConnection class, first splicing conforms to the SOAP protocol XML string information, then establishes the connection with the WebService server, sends the HTTP request, receives the return information to print out. Process and SOAPUI Debugging tools of the same, the following provides the code of the test program, you can refer to.

Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import java.net.HttpURLConnection;

Import Java.net.URL; public class HTTP {public String httpget (string httpurl) {string reqmsg = ' <soapenv:envelope xmlns:soapenv=\ ' http ://www.w3.org/2003/05/soap-envelope\ ">" + "<soapenv:Header>" + "<ns1:username xmlns:ns1=\" Http://servi Ce.webservice.ultrapower.com.cn\ "soapenv:mustunderstand=\" false\ ">username</ns1:Username>" + "&LT;NS1: Password xmlns:ns1=\ "http://service.webservice.ultrapower.com.cn\" soapenv:mustunderstand=\ "false\" &GT;USERPW </ns1:Password> "+" </soapenv:Header> "+" <soapenv:Body> "+" <ns1:sendxml xmlns:ns1=\ "htt P://service.webservice.ultrapower.com.cn\ ">" + "<ns1:requestXml>" + "<?xml version=\" 1.0\ "encoding=\" Utf-8\ "?>" + "<ReadWhiteList>" + "<srcDeviceType>32</srcDeviceType>" + "&Lt;srcdeviceid>321101</srcdeviceid> "+" <Cmd>0</Cmd> "+" </ReadWhiteList> "+" < /ns1:requestxml> "+" <ns1:busiKey>11</ns1:busiKey> "+" </ns1:SendXML> "+" </soapen
		
		V:body> "+" </soapenv:Envelope> "; 
		URL url = null;
        HttpURLConnection httpurlconn= null;
        StringBuffer datastring = new StringBuffer ("");
		The string temp = new string ();
            try{System.out.println (Httpurl);
            url = new URL (httpurl);
            Httpurlconn= (HttpURLConnection) url.openconnection ();
            Httpurlconn.setdooutput (TRUE);
            Httpurlconn.setrequestmethod ("POST"); Httpurlconn.setconnecttimeout (300*1000);//300s timeout Httpurlconn.setrequestproperty ("User-agent", "Apache-HttpClien
            t/4.1.1 (Java 1.5) ");
            Httpurlconn.setrequestproperty ("Content-type", "application/soap+xml;charset=utf-8;action=\" urn:SendXML\ ")"; OutputStream out = HTTPURlconn.getoutputstream ();
            Out.write (Reqmsg.tostring (). GetBytes ());
            Httpurlconn.connect ();
            InputStream in =httpurlconn.getinputstream ();
            BufferedReader bd = new BufferedReader (new InputStreamReader (in));
            while ((Temp=bd.readline ())!=null) {datastring.append (New String (Temp.getbytes ()));
            } in.close ();
        Bd.close ();
        catch (Exception e) {e.printstacktrace ();
            } finally{if (httpurlconn!=null) {httpurlconn.disconnect ();
        } try{System.out.println (New String (Datastring.tostring (). GetBytes ("GBK"), "Utf-8"));
        }catch (Exception e) {e.printstacktrace ();
	return datastring.tostring ();
		public static void Main (string[] arg {http PM = new http ();
	
	Pm.httpget ("http://132.xx.xxx.xx:8080/xxx//xxx?wsdl");
 }
}

There is no feeling of stitching this XML, a little bit complicated. This WebService server soaphead security mechanism, no way, the client asked to do so. Then I'll introduce the WebService server with Soaphead security mechanism.

Reprint please explain from whilejolly:http://blog.csdn.net/seedingly/article/details/39054609

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.