【问题标题】:Passing Credentials to a Web Service using ASP.NET from PHP Sample Code通过 PHP 示例代码使用 ASP.NET 将凭据传递给 Web 服务
【发布时间】:2013-09-21 23:43:47
【问题描述】:

我正在尝试在 ASP.NET 应用程序中使用 Despatch Bay 运输 API,但他们的所有示例代码都在 PHP 中。

我以前使用过网络服务,但无法将凭据传递给它。

PHP 示例代码创建一个新的 API 实例,如下所示:

$soapOptions = array ('login' => APIUSER, 'password' => APIKEY);
$DespatchBay = new SoapClient("http://api.despatchbaypro.com/api/soap/v10/addressing?wsdl",$soapOptions);

使用 Visual Web developer Express,我为我的项目添加了一个服务引用,现在可以像这样创建一个实例:

Dim dbService As New DespatchBayAddresses.AddressingServicePortTypeClient

并尝试从邮政编码中写出地址,如下所示:

Response.Write(dbService.GetDomesticAddressKeysByPostcode("INSERT POSTCODE").ToString())

但是,很自然,我得到一个错误:

The remote server returned an error: (401) Unauthorized.

我知道为什么,因为我没有传递我的 API_KEYAPI_USER 凭据。

但是,我敢肯定,我很愚蠢,但不知道怎么做。

我试过了

dbService.ClientCredentials.UserName.UserName = MY_USER
dbService.ClientCredentials.UserName.Password = MY_KEY

但同样的错误。我需要能够从从我的数据库中检索到的变量中传递这些值,而不是 web.config 中的静态值,所以有人可以指出将这些详细信息传递给 API 的方向吗?

谢谢

【问题讨论】:

    标签: php asp.net web-services credentials


    【解决方案1】:

    好吧……好吧,问题在于我使用了错误类型的 Web 服务。

    终于找到了:

    http://markfreedman.com/index.php/2010/10/13/web-reference-vs-service-reference/

    这为我指明了正确的方向,使用那里提到的步骤添加了参考,现在有了这个(工作)代码:

    Function getAddressesByPostcode(User As String, Api_Key As String, Postcode As String) As Dictionary(Of String, String)
        Dim d As New com.despatchbaypro.api.AddressingService
        d.Credentials = New NetworkCredential(User, Api_Key)
        Dim Addresses = d.GetDomesticAddressKeysByPostcode(Postcode)
        Dim dictionary As New Dictionary(Of String, String)
        For Each address In Addresses
            'Dim add As com.despatchbaypro.api.AddressDetailType = d.GetDomesticAddressByKey(address.Key)
            dictionary.Add(address.Key, address.Address)
        Next
        Return dictionary
    End Function
    

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多