2015-11-04 93 views
3

我创建了以下测试应用程序以检索Service Now中的数据。Web服务没有以正确的格式返回数据

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

    } 
} 

这现在正确连接到服务应用程序没有错误,但是当我尝试打印检索到的数据它给了我钥匙,而不是字符串类型的答案。

enter image description here

对于某些属性它给正确的值(例如sys_updated_by)

enter image description here

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

+0

嗨Sandaru,你能提供一些更多的细节吗?具体的URL是你要求吗? HTTP请求是什么样的?您是否从WSDL生成ServiceNowSoapClient? – Bryan

+0

yah即时通讯使用WSDL。该网址是https://XXXXXXXX.service-now.com/service_subscribe_sys_user_list.do?WSDL – Sandaru

+1

谢谢,仍然可以使用更多的信息来说肯定,但我猜测更新你拉WSDL的URL包括'displayvalue = all'(参考:http://wiki.servicenow.com/?title= Direct_Web_Services#Return_Display_Value_for_Reference_Variables)将有所帮助。如果您可以捕获您的C#应用​​程序发送的示例HTTP请求并将其包含在此处的问题中,它也将有所帮助。如果你这样做,请确保你删除标题中的任何凭据。 – Bryan

回答

3

,你需要做的方式。

如果您在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 =所有的URL。

我们不能用自己的XML编辑端点url,而是可以删除URL并将其添加为关键值。但你仍然不能添加URL参数与&标志你需要的值存储作为单独的按键,并结合其withing程序获取完整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> 

你可以请按如下步骤组装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));