Call WebService with Java HTTP POST emulation SOAPUI

Source: Internet
Author: User
Tags http post soap soapui

The work requires Java to call the WebService interface provided by PeopleSoft, but the PeopleSoft implementers do not provide the Java call case code, SOAPUI can be called, but the Java code has not been written, self-learning and transferred through the Java HTTP Post Simulation method, thanks to Tianya zhouyun0243 and Google!


Import Java.io.bufferedreader;import java.io.file;import Java.io.filereader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.httpurlconnection;import java.net.URL;// Jar package to be Commons-io import Org.apache.commons.io.ioutils;public class TestWebService {public static void main (string[] args) {try {soapspecialconnection ();} catch (Exception e) {e.printstacktrace ()}} public static void Soapspecialconnection () throws exception{string s = new String (); StringBuilder SoapHeader = new StringBuilder ();//soapui automatically generated request XML path, write incoming parameter file File = new file ("D:\\1.xml"); BufferedReader reader = null;try {reader = new BufferedReader (new FileReader (file)); String tempstring = null;//reads one line at a time until NULL is read into the file end while ((tempstring = Reader.readline ()) = null) {Soapheader.append ( tempstring);} Reader.close ();} catch (IOException e) {e.printstacktrace ();} finally {if (reader! = null) {try {reader.close ();} catch (IOException E1) {} }}system.out.println ("soapheader=" +soapHeader)///Set the related properties of the SOAP request message//url from the SOAPUI's Request1 raw label post, without a space in the URL string url= "http://10.60.217.86:9527/ dji-hrservice/services/hrservice.hrservicehttpsoap11endpoint/http/1.1 "; URL u = new url (URL); HttpURLConnection conn = (httpurlconnection) u.openconnection (); Conn.setdoinput (true); Conn.setdooutput (true); Conn.setusecaches (false); Conn.setdefaultusecaches (false);//host,content-type, SOAPAction from SOAPUI's request1 raw tag host,content-typ,soapactione get Conn.setrequestproperty ("Host", " 10.60.217.86:9527 "), Conn.setrequestproperty (" Content-type "," Ext/xml;charset=utf-8 "); Conn.setrequestproperty (" Content-length ", String.valueof (Soapheader.length ())) Conn.setrequestproperty (" SOAPAction "," urn: Getworkattendancebyuidanddate "); Conn.setrequestmethod (" POST ");//define output stream outputstream = Conn.getoutputstream () ; if (null! = SoapHeader) {byte[] b = soapheader.tostring (). GetBytes ("Utf-8");//Send SOAP request message Output.write (b, 0, b.length);} Output.flush (); Output.close ();//define input stream, get SOAP response message inputstream input = Conn.getinputStream ();//need to set the encoding format, otherwise it will garbled s=ioutils.tostring (input, "UTF-8"); Input.close (); SYSTEM.OUT.PRINTLN ("Output of xml=" +s);}}
SOAPUI's Request1 's raw tag signal



Call WebService with Java HTTP POST emulation SOAPUI

Related Article

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.