使用 c#.Net 读取 WebService Wsdl
Reading WebService Wsdl using c#.Net
我们有一个基于 SOAP 的 Web 服务,当我们在浏览器中输入 url 时,我们能够读取它的 wsdl。我们坐在我们网络中的代理后面,但它没有阻止任何东西,当我们在浏览器中输入 url 时,我们总是能够使用 browser.But 读取 wsdl 说 http://ist.services/CoreServices/coreservices?wsdl
它要求输入用户名和密码这与我的 windows 凭据不同。因此,当我输入开发团队共享的用户名和密码时,它 returns wsdl 页面。请注意,此 Web 服务是在基于 java 的服务器上开发和部署的。
如何在 c#.net 代码中执行相同的操作以及如何在 DiscoveryClientProtocol 中传递安全凭证?我尝试了下面的代码,它适用于不需要安全凭证的网络服务。
// 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);
}
}
由于代码对我帮助不大,当我在浏览器中手动读取 wsdl 时,我打开了 fiddler 来跟踪 http 调用,我看到它采用我输入的凭据作为 "Authorization: Basic cGDFDdsfdfsdsfdsgsgfg="。在 fiddler 中,我看到三个响应分别为 401,302 和 200 的调用。但是在我的 c#.net 代码中,我没有得到 200 响应,它总是抛出 404 错误。
我对此进行了进一步调试,在客户端对象的 httpresponse 中,我看到标志状态为 INVOCATION_FLAGS_INITIALIZED | INVOCATION_FLAGS_NEED_SECURITY
看来我需要将凭据作为安全凭据而不是网络凭据传递。
下面的代码已经解决了这个问题。
CredentialCache myCredentialCache = new CredentialCache { { new Uri(sourceUrl),
"Basic", networkCredential } };
discoveryClientProtocol.AllowAutoRedirect = true;
discoveryClientProtocol.Credentials = myCredentialCache;
我们有一个基于 SOAP 的 Web 服务,当我们在浏览器中输入 url 时,我们能够读取它的 wsdl。我们坐在我们网络中的代理后面,但它没有阻止任何东西,当我们在浏览器中输入 url 时,我们总是能够使用 browser.But 读取 wsdl 说 http://ist.services/CoreServices/coreservices?wsdl
它要求输入用户名和密码这与我的 windows 凭据不同。因此,当我输入开发团队共享的用户名和密码时,它 returns wsdl 页面。请注意,此 Web 服务是在基于 java 的服务器上开发和部署的。
如何在 c#.net 代码中执行相同的操作以及如何在 DiscoveryClientProtocol 中传递安全凭证?我尝试了下面的代码,它适用于不需要安全凭证的网络服务。
// 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);
}
}
由于代码对我帮助不大,当我在浏览器中手动读取 wsdl 时,我打开了 fiddler 来跟踪 http 调用,我看到它采用我输入的凭据作为 "Authorization: Basic cGDFDdsfdfsdsfdsgsgfg="。在 fiddler 中,我看到三个响应分别为 401,302 和 200 的调用。但是在我的 c#.net 代码中,我没有得到 200 响应,它总是抛出 404 错误。
我对此进行了进一步调试,在客户端对象的 httpresponse 中,我看到标志状态为 INVOCATION_FLAGS_INITIALIZED | INVOCATION_FLAGS_NEED_SECURITY
看来我需要将凭据作为安全凭据而不是网络凭据传递。
下面的代码已经解决了这个问题。
CredentialCache myCredentialCache = new CredentialCache { { new Uri(sourceUrl),
"Basic", networkCredential } };
discoveryClientProtocol.AllowAutoRedirect = true;
discoveryClientProtocol.Credentials = myCredentialCache;