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?
 
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#.
0
 
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.
0
 
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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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.
0
 
iJavaAuthor Commented:
How would I go about trying that? Do you have an example?
0
 
Bob LearnedCommented:
I would think that you need to use the same request, and get multiple responses.  Are the two request URLs the same?
0
 
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.
0
 
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.
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.