从c#调用PHP Web服务

时间:2014-07-18 09:29:39

标签: c# php asp.net web-services

我目前正在尝试用c#调用PHP Web服务。我一直在尝试在互联网上找到的几十种解决方案,但没有运气,没有一个与我有同样的问题。我不熟悉PHP。

我可以从c#

成功调用authenticate_get
string auth_id = client.authenticate_get("www.testexample.com", "e5d30c56d600a7456846164");

并返回验证ID,但后来不知道如何在c#中传递数组。这是给出的PHP示例。

<?php
   $client = new SoapClient("https://example.com/TESTApi_v1_1.wsdl.php");
   $auth_id = $client->authenticate_get('username', 'password');
   $client = new SoapClient("https://example.com/TESTApi_v1_1.wsdl.php", array("login" => $auth_id ));
 ?>

当我尝试调用任何其他方法时,我只是收到错误,返回“需要HTTP Basic Authorization标头”。

我也尝试过:

Uri uri = new Uri(url);
            ICredentials credentials = netCredential.GetCredential(uri, "Basic login:" + auth_id);
        client.Credentials = credentials;
            client.PreAuthenticate = true;

我一直在努力:

public class MyHeader : SoapHeader
{
    public string login;
}

[WebService(Namespace = "https://example.com/TESTApi_v1_1.wsdl.php")]
public class exampleTestService : ExampleService
{
    public MyHeader myOutHeader;

    [WebMethod]
    [SoapHeader("login", Direction = SoapHeaderDirection.InOut)]
    public MyHeader MyOutHeaderMethod()
    {
        var client = new ExampleService();
        string auth_id = client.authenticate_get("www.testexample.com", "e5d30c56d600a7e6f3cef05d8c0a1991");
        // Return the client's authenticated name.
        MyHeader outHeader = new MyHeader();
        outHeader.login = auth_id;
        return outHeader;
    }
}

我确信我错过了一些简单的事情。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

我有它的工作。万一其他人可以找到我的答案有用:

 public partial class TheWebServiceSubClass : ExampleService
{
    protected override WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);
        ExampleService client = new ExampleService();
        string auth_id = client.authenticate_get("www.testexample.com", "e5d30c56d600a7456846164");
        string credentials =
            Convert.ToBase64String(Encoding.ASCII.GetBytes("www.testexample.com:e5d30c56d600a7456846164"));
        string credentials1 = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth_id+ ":"));
        webRequest.Headers["Authorization"] = "Basic " + credentials1;
        return webRequest;
    }
}