我目前正尝试在 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/