Web 服务未以正确格式返回数据

Web service not returning data in correct format

我创建了以下测试应用程序以从 Service Now.

检索数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace ServiceNowConnection
{
    class Program
     {
         static void Main(string[] args)
        {
            Service_Now_Reference.getRecords records = new Service_Now_Reference.getRecords();
            records.sys_user = "XXXXXXX";

            Service_Now_Reference.ServiceNowSoapClient proxyUser = new Service_Now_Reference.ServiceNowSoapClient();
            proxyUser.ClientCredentials.UserName.UserName = "XXXXXX";
            proxyUser.ClientCredentials.UserName.Password = "XXXXXX";
            Service_Now_Reference.getRecordsResponseGetRecordsResult[] result = proxyUser.getRecords(records);

            foreach (var item in result)
            {
                Console.WriteLine(item.sys_created_by);
            }
            Console.ReadLine();
        }

    }
}

这会正确连接到 service now 应用程序,没有错误,但是当我尝试打印检索到的数据时,它会给我密钥而不是字符串类型的答案。

对于某些属性,它给出了正确的值(示例 sys_updated_by)

如何避免这种情况。

如果您更新用​​于获取 WSDL 并向其发出请求的 URL,以包括 'displayvalue=all',响应将包括显示名称以及 sys_id 值(想想foreign_key) 个引用记录。

有关更多信息,请查看:http://wiki.servicenow.com/?title=Direct_Web_Services#Return_Display_Value_for_Reference_Variables&gsc.tab=0

谢谢, 布莱恩

您需要的方式。

如果您在 C# 中使用 RESTfull API,您现在可以通过直接向服务发送 HTTPS 调用来实现。

但是当您使用 SOAP 时事​​情会变得复杂 api

当您将 Web 服务导入您的程序时,它包括以下 XML 代码到 App.config 或 Web.config,具体取决于您的应用程序意图(Web 应用程序或独立应用程序) .

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="ServiceNowSoap">
      <security mode="Transport" />
    </binding>
    <binding name="ServiceNowSoap1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://XXXXXXXX.service-now.com/service_subscribe_sys_user_list.do?SOAP"
    binding="basicHttpBinding" bindingConfiguration="ServiceNowSoap"
    contract="ServiceReference1.ServiceNowSoap" name="ServiceNowSoap" />
</client>
 </system.serviceModel>

首先,如果您希望检索大量记录,则需要增加“最大接收消息大小”的大小。

为此,您需要像下面那样编辑标签

<binding name="ServiceNowSoap" maxReceivedMessageSize="2000000000">

下一部分是将 displayvalue=all 添加到 URL。

我们无法使用 XML 本身编辑终点 url,您可以删除 URL 并将其添加为键值。但是您仍然无法使用 & 符号向 url 添加参数,您需要将值存储为单独的键并将其与程序组合以获得完整的 URL

最终XML会是这样

<configuration>
  <appSettings>
    <add key="serviceNowUrl"
     value="https://XXXXXXXX.service-now.com/service_subscribe_sys_user_list.do?"/>
    <add key="displayvalue" value="displayvalue=true"/>
    <add key="protocol" value="SOAP"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="ServiceNowSoap" maxReceivedMessageSize="2000000000">
                    <security mode="Transport">
                        <transport clientCredentialType="Basic" proxyCredentialType="Basic"
                        realm="">
                            <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
                <binding name="ServiceNowSoap1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint binding="basicHttpBinding" bindingConfiguration="ServiceNowSoap"
            contract="Service_Now_Reference.ServiceNowSoap" name="ServiceNowSoap" />
        </client>
    </system.serviceModel>
</configuration>

您可以assemble url 如下

    string url = ConfigurationSettings.AppSettings["serviceNowUrl"];
    string protocol = ConfigurationSettings.AppSettings["protocol"];
    string displayvalue = ConfigurationSettings.AppSettings["displayvalue"];

    System.ServiceModel.EndpointAddress endpoint = new System.ServiceModel.EndpointAddress(string.Format("{0}{1}{2}", url, protocol, displayvalue));