从C#调用PHP Web服务 [英] Calling a PHP webservice from c#

查看:65
本文介绍了从C#调用PHP Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to call a PHP web service in c#. I have been trying dozens of solutions I have found on the internet, but with no luck, and none which have the same issue as me. I am unfamiliar with PHP.

我可以从我的C#中成功调用authenticate_get

I can successfully call authenticate_get from my c#

string auth_id = client.authenticate_get("www.testexample.com", "e5d30c56d600a7456846164");

并获得返回的身份验证ID,但随后不知道如何在C#中传递数组.这是给出的PHP示例.

and get the authentication id returned, but then don't know how to pass the array in c#. Here is PHP example given.

<?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基本授权标头".

When I try to call any other methods I am just getting an error returned "HTTP Basic Authorization header required".

我也尝试过:

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

我确定我缺少简单的东西.

I am sure I am missing something simple.

在此先感谢您的帮助.

推荐答案

我已经开始使用它了.如果其他人可以找到我的答案有帮助:

I have got it working. In case anyone else can find my answer helpful:

 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#调用PHP Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆