我们有一个基于SOAP的web服务,当我们在浏览器中输入url时,我们能够读取它的wsdl。我们坐在网络中的代理后面,但它不会阻塞任何东西,当我们在浏览器中输入url时,我们总是能够使用browser.But读取wsdl,比如http://ist.services/CoreServices/coreservices?wsdl,它要求用户名和密码,这与我的windows凭据不一样。因此,当我输入由dev团队共享的用户名和密码时,它将返回wsdl页面。请注意,此and服务是在基于java的服务器上开发和部署的。
如何在c#.net代码中执行相同的操作,以及如何在DiscoveryClientProtocol中传递安全证书?我尝试了下面的代码,它适用于不要求安全凭据的ask服务。
// 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时,我打开了这个工具来跟踪http调用,并且我看到它将我输入的凭据作为“授权:基本cGDFDdsfdfsdsfdsgsgfg=”。在小提琴中,我看到了三个响应401,302和200的呼叫。但是在我的c#.net代码中,我没有得到200个响应,它总是抛出404错误。
我进一步调试了这一点,在客户端对象的httpresponse中,我看到标志状态为INVOCATION_FLAGS_INITIALIZED | INVOCATION_FLAGS_NEED_SECURITY。
因此,我需要将凭据作为安全凭据而不是网络凭据传递。
发布于 2017-04-07 00:09:39
下面的代码解决了这个问题。
CredentialCache myCredentialCache = new CredentialCache { { new Uri(sourceUrl),
"Basic", networkCredential } };
discoveryClientProtocol.AllowAutoRedirect = true;
discoveryClientProtocol.Credentials = myCredentialCache;https://stackoverflow.com/questions/43242365
复制相似问题