简体   繁体   English

创建Java客户端以访问WSHTTPBINDING API服务

[英]Creating Java Client to access WSHTTPBINDING API Service

I need to consume a web service from UltiPro in Java. 我需要使用Java中的UltiPro使用Web服务。 All the examples from UltiPro are for C# (see below) and I cannot see how to translate the into Java. UltiPro的所有示例都是针对C#的(见下文),我看不到如何将其转换为Java。

My research indicates that the key is WSHttpBinding. 我的研究表明关键是WSHttpBinding。

UltiPro's documentation includes an XML file that they say... UltiPro的文档包含一个XML文件,他们说...

The following code is an XML example of authenticating to your UltiPro data. 以下代码是对UltiPro数据进行身份验证的XML示例。 You can copy the entire contents and update the values in the request. 您可以复制整个内容并更新请求中的值。 The response example shows how a successful response will be formatted. 响应示例显示了如何格式化成功的响应。

I have written many Web Services and RESTful programs before, but I am stuck on this one. 我以前写了很多Web服务和RESTful程序,但我坚持这个。

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
<a:Action   s:mustUnderstand="1">http://www.ultipro.com/services/loginservice/ILoginService/Authenticate</a:Action> 
        <h:ClientAccessKey xmlns:h="http://www.ultipro.com/services/loginservice">CAK</h:ClientAccessKey> 
        <h:Password xmlns:h="http://www.ultipro.com/services/loginservice">PASSWORD</h:Password> 
        <h:UserAccessKey xmlns:h="http://www.ultipro.com/services/loginservice">USER API KEY</h:UserAccessKey> 
        <h:UserName xmlns:h="http://www.ultipro.com/services/loginservice">USERNAME</h:UserName> 
      </s:Header>
  <s:Body>
        <TokenRequest xmlns="http://www.ultipro.com/contracts" /> 
  </s:Body>
</s:Envelope>

C# code: C#代码:

namespace ConsoleSample
{
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;

    using ConsoleSample.LoginService;

    public class Program
    {
        internal static void Main(string[] args)
        {
            // Setup your user credentials:
            const string UserName = "";
            const string Password = "";
            const string UserApiKey = "";
            const string CustomerApiKey = "";

            // Create a proxy to the login service:
            var loginClient = new LoginServiceClient("WSHttpBinding_ILoginService");

            try
            {
                // Submit the login request to authenticate the user:
                string message;
                string authenticationToken;

                AuthenticationStatus loginRequest =
                    loginClient
                        .Authenticate(
                            CustomerApiKey,
                            Password,
                            UserApiKey,
                            UserName,
                            out message,
                            out authenticationToken);

                if (loginRequest == AuthenticationStatus.Ok)
                {
                    // User is authenticated and the authentication token is provided.
                    Console.WriteLine("User authentication successful.");
                }
                else
                {
                    // User authentication has failed. Review the message for details.
                    Console.WriteLine("User authentication failed: " + message);
                }

                loginClient.Close();

                Console.WriteLine("Press a key to exit...");
                Console.ReadKey(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex);
                loginClient.Abort();
                throw;
            }
        }

I understand you are looking to write it in JAVA but maybe something from my code will help. 我知道您希望在JAVA中编写它,但也许我的代码中的某些东西会有所帮助。

I was able to successfully authenticate using the following code: 我能够使用以下代码成功进行身份验证:

<?php
$url             = 'https://rental5.ultipro.com/services/LoginService';
$action          = 'http://www.ultipro.com/services/loginservice/ILoginService/Authenticate';
$username        = 'MY_USERNAME';
$password        = 'MY_PASSWORD';
$userAccessKey   = 'MY_USER_ACCESS_KEY';
$clientAccessKey = 'MY_CLIENT_ACCESS_KEY';

$loginPayload =<<<EOD
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">{$action}</a:Action>
        <h:ClientAccessKey xmlns:h="http://www.ultipro.com/services/loginservice">{$clientAccessKey}</h:ClientAccessKey>
        <h:Password xmlns:h="http://www.ultipro.com/services/loginservice">{$password}</h:Password>
        <h:UserAccessKey xmlns:h="http://www.ultipro.com/services/loginservice">{$userAccessKey}</h:UserAccessKey>
        <h:UserName xmlns:h="http://www.ultipro.com/services/loginservice">{$username}</h:UserName>
    </s:Header>
    <s:Body>
        <TokenRequest xmlns="http://www.ultipro.com/contracts" />
    </s:Body>
</s:Envelope>
EOD;

$client = new SoapClient(
    null,
    array(
        'location'     => $url,
        'uri'          => '',
        'trace'        => 1,
        'encoding'     => 'UTF-8',
        'use'          => SOAP_LITERAL,
        'soap_version' => SOAP_1_2
    )
);

try {
    $order_return = $client->__doRequest($loginPayload, $url, $action, SOAP_1_2, 0);

    $getLastRequestHeaders = $client->__getLastRequestHeaders();
    $getLastRequest = $client->__getLastRequest();
    $getLastResponseHeaders = $client->__getLastResponseHeaders();
    $getLastResponse = $client->__getLastResponse();

    echo "<pre>";
    echo $getLastRequestHeaders;
    echo '<br>';
    echo $getLastResponseHeaders;
    echo '<br>';
    print_r($order_return);
    echo "</pre>";

} catch (SoapFault $exception) {
    var_dump(get_class($exception));
    var_dump($exception);
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM