krrishna
krrishna

Reputation: 2078

Reading WebService Wsdl using c#.Net

We have a SOAP based web service and we are able to read its wsdl when we type in the url in Browser. We sit behind a proxy in our network but its not blocking anything and we are always able to read wsdl using browser.But when we enter the url in Browser say http://ist.services/CoreServices/coreservices?wsdl it asks for username and password which is not same as my windows credentials. So when i enter the username and password shared by the dev team , it returns the wsdl page. Please note that this webservice is developed and deployed on java based server.

How do i do the same in c#.net code and how do i pass the Security Crednetials in DiscoveryClientProtocol? I tried the below code which works for the webservices which doesn't ask for the Security credentials.

  // 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);

    }
    }

Since the code didn't help me much , i opened the fiddler to trace the http calls when i manual read the wsdl in browser and i see it takes the credentials i entered as "Authorization: Basic cGDFDdsfdfsdsfdsgsgfg=" . In fiddler i see three calls with responses 401,302 and 200. But in my c#.net code i don't get the 200 response and it always throws me the 404 error.

I further debugged this and in httpresponse of client object i see the flag status as INVOCATION_FLAGS_INITIALIZED | INVOCATION_FLAGS_NEED_SECURITY

So looks like i need to pass the credentials as Security Credentials rather than Network credentials.

Upvotes: 1

Views: 973

Answers (1)

krrishna
krrishna

Reputation: 2078

The below code has fixed the issue.

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

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

Upvotes: 2

Related Questions