用C#调用PHP webservice

3

我目前正在尝试在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基本授权头"。

我也尝试了以下方法:

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

我相信我缺少了一些简单的东西。
提前感谢您的帮助。

你试过这个吗:https://dev59.com/C1jUa4cB1Zd3GeqPSIM3? - Shaharyar
是的,但是我无法弄清楚在调用Web服务时如何实现它。MyService客户端= new MyService(); 没有Headers选项。 - Katie366
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接