2014-07-18 73 views
2

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

我能成功地从我的C#

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

打电话authenticate_get并获得认证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; 
    } 
} 

我相信我失去了一些东西简单。

非常感谢您的帮助。

+0

你有没有尝试过这样的:http://stackoverflow.com/questions/6440255/missing-basic-http-authentication-entry-in-http-request-header ? – Shaharyar

+0

是的,但我无法解决调用web服务时如何实现它。 MyService client = new MyService(); Headers没有选项 – Katie366

回答

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