【问题标题】:Web service not returning data in correct formatWeb 服务未以正确格式返回数据
【发布时间】:2016-02-04 12:32:59
【问题描述】:

我创建了以下测试应用程序来从 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)

我怎样才能避免这种情况。

【问题讨论】:

  • 您好 Sandaru,您能提供更多详细信息吗?您正在向具体的 URL 发出请求吗? HTTP 请求是什么样的?您是否从 WSDL 生成了 ServiceNowSoapClient?
  • 是的,我正在使用 WSDL。网址是XXXXXXXX.service-now.com/…
  • 谢谢,仍然可以使用更多信息来确定,但我猜更新您从中提取 WSDL 的 URL 以包含“displayvalue=all”(请参阅​​:wiki.servicenow.com/…)会有所帮助。如果您可以捕获您的 C# 应用程序发送的示例 HTTP 请求并将其包含在此处的问题中,这也会有所帮助。如果您这样做,请确保删除标题中的所有凭据。
  • 谢谢我在你解释的同时发现了错误。这正是需要做的事情。您能否将其评论为答案。

标签: c# web-services servicenow


【解决方案1】:

如果您更新用​​于获取 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

谢谢, 布莱恩

【讨论】:

    【解决方案2】:

    你需要做的方式。

    如果您在 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>
    

    首先,如果您希望检索大量记录,则需要增加 Max Received Message Size 的大小。

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

    <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>
    

    你可以按如下方式组装网址

        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));
    

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多