Reading XML in ASP.Net

Please bear with me, this gets messy :-D
I have a project and I'm at a dead end. I am provided with a set of API's that I can use to query a server via URL with querystring and it will return a result as XML.

In the first API I pass in my username and password to authenticate myself with the server.
Once I have authenticated successfully the API will start a new browser session and return the session id in the XML allowing me to execute additional API requests. There is also a node "status" in the xml which returns success/failure for the authentication. I use XPath to get that value and on "success" I execute the next API.

Now the second API. In this one I pass a user's email address and because I have already authenticated the server returns a "userid" in XML format. I need to use XPath to get that value as well.

Here is the problem. When I call the second API, my app is dropping the session and so it fails because I am no longer authenticated.

Any ideas?

Attached is some code.
Please let me know if you need anything else and THANK YOU in advance for your help.
private void bhiveAuthenticate(string em)
    {
        String strResult = "";
 
        string fileName = "https://secure.test.com/bhive/main/user/authenticate?login=admin@test.com&password=password&tenant_id=1004";
        XPathDocument doc = new XPathDocument(fileName);
        XPathNavigator nav = doc.CreateNavigator();
 
        // Compile a standard XPath expression
        XPathExpression expr;
        expr = nav.Compile("/response/status/@code");
        XPathNodeIterator iterator = nav.Select(expr);
 
        // Iterate on the node set
        try
        {
            while (iterator.MoveNext())
            {
                XPathNavigator nav2 = iterator.Current.Clone();
                Label1.Text = nav2.Value.ToString();
            }
 
            // Action if successful
            string fileName2 = "https://secure.test.com/bhive/main/user/getuserid?login=testuser@test.com";
            doc = new XPathDocument(fileName2);
            nav = doc.CreateNavigator();
 
            // Compile a standard XPath expression
            XPathExpression expr2;
            expr2 = nav.Compile("/response/status/@code");
            XPathNodeIterator iterator2 = nav.Select(expr2);
 
            // Iterate on the node set
            try
            {
                while (iterator2.MoveNext())
                {
                    XPathNavigator nav4 = iterator2.Current.Clone();
                    Label2.Text = nav4.Value.ToString();
                }
            }
            catch (Exception ex2)
            {
                Response.Write(ex2.Message);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

Open in new window

iJavaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I would think that you might be able to use an HttpWebRequest, get the response text, create an XmlDocument, and parse the response with XPath fairly easily.
iJavaAuthor Commented:
I have tried HttpWebRequest and got similar results. I request one API after the other and I receive a new browser session with each request.

My code is attached below. Here is the result displayed when I run it.
Result of the 1st API:
version="1.0" ******** jeevanall@mentorware.com 1004 8612CD462D099A997ED172676364770C.app2ec2success 33049 1004 jeevanall@mentorware.com jeevanall@mentorware.com Jeevan Kumar #38, II cross Gangappa Block Ganganagar Bangalore Karnataka 560032 United States 23434861 bHiveSoft Aerospace and Defense Account Exec N Y 155 1225998321848 Y 1219453085000 5729 Jeevan Kumar 1224823897000 33049 Jeevan Kumar PD0733049 true false false User_Type Multiple Choice 1984 Customer User_Type Multiple Choice 1985 Lenovo Employee Support Center Boolean true Language_Preference Multiple Choice 2006 English Location_Id Text 1111111111 User_Type Multiple Choice 1986 Business Partner

Result of the 2nd API:
version="1.0" jeevan4@mentorware.com 0014DAF4C8BE3F8E9E06888DDF63BA92.app2ec2Insufficient privileges: Not authorized to get bhive user id

You'll note that at the end of the second request I receive the message "Not authorized to get bhive user id". This should not be the case.

Alsoo note in the response the browser session for the first call is "8612CD462D099A997ED172676364770C" and for the second API call the session id is "0014DAF4C8BE3F8E9E06888DDF63BA92". Makes no sense to me.

The strange thing is that if I open a browser and paste the request URL's into the address bar one after another it works fine.

Any other ideas?
protected void Page_Load(object sender, EventArgs e)
    {
        // Request the 1st API
        HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create("https://secure.test.com/bhive/main/user/authenticate?login=test@test.com&password=password&tenant_id=1004");
        webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
        webRequest.Accept = "text/xml";
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
        Stream responseStream = webResponse.GetResponseStream();
        XmlTextReader reader = new XmlTextReader(responseStream);
        Response.Write("Result of the 1st API:<br />");
        while(reader.Read())
        {
            // Write out the result of 1st API
            Response.Write(reader.Value.ToString());
        }
        reader.Close();
        webResponse.Close();
 
        // Request the 2nd API
        HttpWebRequest webRequest2 = (HttpWebRequest)System.Net.WebRequest.Create("https://secure.test.com/bhive/main/user/getuserid?login=test@test.com");
        webRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
        webRequest2.Accept = "text/xml";
        HttpWebResponse webResponse2 = (HttpWebResponse)webRequest2.GetResponse();
        Stream responseStream2 = webResponse2.GetResponseStream();
        XmlTextReader reader2 = new XmlTextReader(responseStream2);
        Response.Write("<br /><br />Result of the 2nd API:<br />");
        while (reader2.Read())
        {
            // Write out the result of 1st API
            Response.Write(reader2.Value.ToString());
        }
        reader2.Close();
        webResponse2.Close();
 
    }

Open in new window

ChetOS82Commented:
I suspect that the server is using the session to maintain state, therefore you have to copy the returned cookies from the first call to the second call.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

iJavaAuthor Commented:
How would I go about trying that? Do you have an example?
Bob LearnedCommented:
I would think that you need to use the same request, and get multiple responses.  Are the two request URLs the same?
iJavaAuthor Commented:
WOW, It can't be done?

I cannot assign a solution to this problem. "I would think" it could have been done in C# but I had to code it in JS to make my deadline. If someone can provide a workind code example I can assign points. But at this juncture I have to say you just can't do it in C#.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
I don't believe that I said that it can't be done, I just think that you need to keep one HttpWebRequest, and reuse it, instead of creating a new HttpWebRequest.
iJavaAuthor Commented:
Ok, can you show me what that looks like working? I have not been able to execute a second HttpWebRequest URL without the use of the word "new", this unfortunately creates  anew session as well.
Bob LearnedCommented:
Look through this article, and you will notice that there is one HttpWebRequest object, which uses PreAuthenticate.  I believe that is the direction that you need to take.

A POST or PUT request may fail when you use the HttpWebRequest class to send lots of data on a computer that is running the .NET Framework  
http://support.microsoft.com/kb/908573
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.