[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading XML in ASP.Net

Posted on 2008-11-05
9
Medium Priority
?
501 Views
Last Modified: 2013-11-11
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

0
Comment
Question by:iJava
  • 4
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22896089
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
 

Author Comment

by:iJava
ID: 22898437
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
 
LVL 18

Expert Comment

by:ChetOS82
ID: 22898460
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:iJava
ID: 22899015
How would I go about trying that? Do you have an example?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22899840
I would think that you need to use the same request, and get multiple responses.  Are the two request URLs the same?
0
 

Accepted Solution

by:
iJava earned 0 total points
ID: 22946903
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22949504
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
 

Author Comment

by:iJava
ID: 22956800
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22956989
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Loops Section Overview
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question