c# - 从 C# 调用 PHP Web 服务

标签 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 基本授权 header ”。

我也试过:

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

我确信我遗漏了一些简单的东西。

提前感谢您的帮助。

最佳答案

我已经开始工作了。如果其他人发现我的回答有帮助:

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

关于c# - 从 C# 调用 PHP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24821637/

相关文章:

c# - 无法将 ObservableCollection 绑定(bind)到 DataGrid

C#:静态基类方法 stub ,在所有派生类中具有*必需*实现

c# - 评估选择投影

asp.net - IE7 不自动显示滚动条?

c# - jQuery 是在 C#.net 环境中处理用户交互的最佳方式吗?

c# - 如何使用 TPL 处理长时间运行的进程

php - 将策略模式与环境变量 Laravel 一起使用

php - 返回与 preg_match 的匹配项,不带数字键的项目

php - 用户注册后如何使Drupal重定向到页面

javascript - 如何触发附加到我的页面的内联 Javascript? (ASP.NET 网络表单)