user22686339
user22686339

Reputation: 1

Send XML Request to webservice

I am attempting to send a call with xml data to a specific webservice but receiving response code 500. I'm struggling to resolve this problem. Any input would be much appreciated.

String wsURL = "https://wd2-impl-services1.workday.com/ccx/service/Payteam/v10.2?wsdl"; URL url = null; URLConnection connection = null; HttpURLConnection httpConn = null; String responseString = null; String outputString=""; OutputStream out = null; InputStreamReader isr = null; BufferedReader in = null;``

String xmlInput ="XMLdocument"

try { url = new URL(wsURL); connection = url.openConnection(); httpConn = (HttpURLConnection) connection;`

byte[] buffer = new byte[xmlInput.length()];`` buffer = xmlInput.getBytes();`

String SOAPAction = ""; //Set the appropriate HTTP parameters\Properties. httpConn.setRequestProperty("Content-Length", String.valueOf(buffer.length)); httpConn.setRequestProperty("Content-Type","text/xml; charset-utf-8"); httpConn.setRequestProperty("Username","username"); httpConn.setRequestProperty("Password", "password");`

httpConn.setRequestProperty("SOAPAction",SOAPAction); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); out = httpConn.getOutputStream(); out.write(buffer); out.close();

//Read Response isr = new InputStreamReader(httpConn.getInputStream()); in = new BufferedReader(isr); while ((responseString = in.readLine()) != null) { outputString = outputString + responseString;

} System.out.println(outputString); System.out.println("");

Document document = parseXmlFile(outputString); }`` catch (Exception e){e.printStackTrace(); }`

}

private static Document parseXmlFile(String in) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(in)); return db.parse(is); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e);

}

Upvotes: 0

Views: 307

Answers (1)

Nikolaj Hansen
Nikolaj Hansen

Reputation: 255

You need to generate a client based on the wsdl file you link to above using JAXB. Then use said client to communicate with the SOAP web service.

This is already described a million times online, so I will link to a good explanation here:

https://www.baeldung.com/java-soap-web-service

Edit: seems a little more help is needed - here is a maven project file to generate your client. However - the wsdl download url seems to not provide me with a working wsdl.

curl https://wd2-impl-services1.workday.com/ccx/service/Payteam/v10.2?wsdl
400 : Invalid request

Build file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.foo.client.wsi</groupId>
  <artifactId>WorkdayClient</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>WorkdayClient</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
      <dependency>
              <groupId>jakarta.xml.ws</groupId>
        <artifactId>jakarta.xml.ws-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>3.0.0</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-ri</artifactId>
        <version>3.0.0</version>
        <type>pom</type>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
      <plugins>
              <plugin>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <wsdlUrls>
                    <wsdlUrl>https://wd2-impl-services1.workday.com/ccx/service/Payteam/v10.2?wsdl</wsdlUrl>
                </wsdlUrls>
                <keep>true</keep>
                <packageName>com.foo.soap.ws.client.generated</packageName>
                <sourceDestDir>src/main/java</sourceDestDir>
            </configuration>
        </plugin>
      </plugins>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Upvotes: 0

Related Questions