2014-07-18 70 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; 
    } 
}