2017-04-05 97 views
1

我們有一個基於SOAP的Web服務,當我們在瀏覽器中輸入url時,我們能夠讀取它的wsdl。我們坐在我們網絡的代理服務器後面,但它沒有阻塞任何東西,而且我們總是能夠使用瀏覽器閱讀wsdl。但是當我們在瀏覽器中輸入網址http://ist.services/CoreServices/coreservices?wsdl時,它會要求輸入與我的Windows憑據不同的用戶名和密碼。所以當我輸入開發團隊共享的用戶名和密碼時,它會返回wsdl頁面。請注意,這個web服務是在基於java的服務器上開發和部署的。使用c#.Net讀取WebService Wsdl網址

如何在c#.net代碼中執行相同的操作,以及如何在DiscoveryClientProtocol中傳遞安全信用?我嘗試了下面的代碼,它適用於不需要安全證書的Web服務。

// Specify the URL to discover. 
     string sourceUrl = "http://ist.services/CoreServices/coreservices?wsdl"; 

     string outputDirectory = "C:\\Temp"; 

     DiscoveryClientProtocol client = new DiscoveryClientProtocol(); 
     var credentials = new NetworkCredential("sunuser1", "xxxxxxx", ""); 

     WebProxy proxy = new WebProxy("http://proxy.bingo:8000/", true) { Credentials = credentials }; 

      client.Credentials = credentials; 
     // Use default credentials to access the URL being discovered. 
     //client.Credentials = credentials;//CredentialCache.DefaultCredentials; 
     client.Proxy = proxy; 
     String DiscoverMode = "DiscoverAny"; 
     String ResolveMode = "ResolveAll"; 
     try 
     { 
      DiscoveryDocument doc; 
      // Check to see if whether the user wanted to read in existing discovery results. 
      if (DiscoverMode == "ReadAll") 
      { 
       DiscoveryClientResultCollection results = client.ReadAll(Path.Combine("C:\\Temp", "results.discomap")); 
       //SaveMode.Value = "NoSave"; 
      } 
      else 
      { 
       // Check to see if whether the user wants the capability to discover any kind of discoverable document. 
       if (DiscoverMode == "DiscoverAny") 
       { 
        doc = client.DiscoverAny(sourceUrl); 
       } 
       else 
       // Discover only discovery documents, which might contain references to other types of discoverable documents. 
       { 
        doc = client.Discover(sourceUrl); 
       } 
       // Check to see whether the user wants to resolve all possible references from the supplied URL. 
       if (ResolveMode == "ResolveAll") 
        client.ResolveAll(); 
       else 
       { 
        // Check to see whether the user wants to resolve references nested more than one level deep. 
        if (ResolveMode == "ResolveOneLevel") 
         client.ResolveOneLevel(); 
        else 
         Console.WriteLine("empty"); 
       } 
      } 
     } 
     catch (Exception e2) 
     { 
      //DiscoveryResultsGrid.Columns.Clear(); 
      //Status.Text = e2.Message; 
      Console.WriteLine(e2.Message); 
     } 
     // If documents were discovered, display the results in a data grid. 
     if (client.Documents.Count > 0) 
      Console.WriteLine(client); 

    } 
    } 

由於代碼沒有幫助我很多,我打開小提琴手跟蹤HTTP調用當我閱讀手冊在瀏覽器WSDL和我看到它需要我輸入爲「授權憑證:基本cGDFDdsfdfsdsfdsgsgfg = 「。在提琴手中,我看到三個響應401,302和200.但在我的c#.net代碼中,我沒有得到200響應,它總是會引發404錯誤。

我進一步調試這和客戶對象的HttpResponse我看到的INVOCATION_FLAGS_INITIALIZED | INVOCATION_FLAGS_NEED_SECURITY

標誌狀態所以看起來像我需要傳遞憑證的安全憑據,而不是網絡憑據。

回答

2

以下代碼已修復此問題。

 CredentialCache myCredentialCache = new CredentialCache { { new Uri(sourceUrl), 
       "Basic", networkCredential } }; 

     discoveryClientProtocol.AllowAutoRedirect = true; 
     discoveryClientProtocol.Credentials = myCredentialCache;