简体   繁体   中英

Getting a XML file as response in C# from Azure

We have a RDL and XML file on Azure. I have a fullURI to a path with loginId and password (of Azure) which can download a xml file directly.

In a browser, this link (fullURI) will 1. Open Azure Login Page. 2. On entering the credentials it opens up a dialog that will ask you - Do you want to Open / Save / Cancel the xml file? 3. If we select Open then it will open the XML file in the browser.

In WP7 application with C# code we are trying to access the xml from above. The code snippet is as given below.

protected class GetContext
{
    public HttpWebRequest Request;
}

Request

HttpWebRequest xmlReportRequest;

… …. 
// baseuri is a HTTPS link to azure reporting server.
URI fullUri = new Uri(baseuri + "&rs:Command=Render&rs:Format=XML"); 
xmlReportRequest = (HttpWebRequest)HttpWebRequest.Create(fullUri.Uri);
xmlReportRequest.Credentials = new NetworkCredential(UserName, Password);

// start the asynchronous request
xmlReportRequest.BeginGetResponse(new AsyncCallback(HandleResponse),    new GetContext {   Request = request });

);

Response

private void HandleResponse(IAsyncResult asyncResult)
{
    var context = (GetFContext)result.AsyncState;
    WebResponse response = context.Request.EndGetResponse(asyncResult);

    //get the stream containing the response from the async call
    Stream streamResult;
    streamResult  =  response.GetResponseStream();

    // load the XML
    _xmlDocument = XElement.Load(streamResult); // EXCEPTION AT THIS LINE NOTSUPPORTEDEXCEPTION

   ….   …         …

}

Here, the output of GetResponseStream is not a XML file. When the stream is stored into a string we see that it is the Azure login page as a HTML.

RESPONSE FROM GETRESPONSESTREAM which I feel is just a HTML azure login page (with the information that we sent). Below is the response, here i have replaced some confidential information with URL_TO_THE_XML_FILE_IN_REPORTING_SERVER (it is basically target uri), SOME_ENCRYPTED_VALUE.

\r\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" >\r\n\r\n\r\n<html lang=\"en-US\">\r\n   <head id=\"Head1\"><meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\" /><link rel=\"Stylesheet\" type=\"text/css\" href=\"Public/Logon.css\" /><title>\r\n\tWindows Azure SQL Reporting\r\n</title></head>\r\n   <body >\r\n      <form name=\"Logon\" method=\"post\" action=\"logon.aspx?ReturnUrl=URL_TO_THE_XML_FILE_IN_REPORTING_SERVER\" id=\"Logon\">\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/SOME_ENCRYPTED_VALUE\" />\r\n\r\n<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"/SOME_ENCRYPTED_VALUE\" />\r\n\r\n        <h1 id=\"Title\"><span id=\"LblTitleContent\">Windows Azure</span></h1>\r\n        <hr />\r\n        <div class=\"ClsIntro\">\r\n            <span id=\"LblIntro\"></span>\r\n        </div>\r\n        <div class=\"ClsInput\">\r\n            <div>\r\n                <label for=\"TxtUser\" id=\"LblUser\">User name</label>\r\n            </div>\r\n            <div>\r\n                <input name=\"TxtUser\" type=\"text\" id=\"TxtUser\" tabindex=\"1\" />\r\n            </div>\r\n        </div>\r\n        <div class=\"ClsInput\">\r\n            <div>\r\n                <label for=\"TxtPwd\" id=\"LblPwd\">Password</label>\r\n            </div>\r\n            <div>\r\n                <input name=\"TxtPwd\" type=\"password\" id=\"TxtPwd\" tabindex=\"2\" />\r\n            </div>\r\n        </div>\r\n        <div class=\"ClsSignIn\">\r\n            <input type=\"submit\" name=\"BtnLogon\" value=\"Sign in\" id=\"BtnLogon\" tabindex=\"4\" /><input type=\"image\" name=\"BtnLogonArrow\" id=\"BtnLogonArrow\" tabindex=\"5\" src=\"Public/WhiteRightArrow.png\" alt=\"Sign in\" align=\"baseline\" border=\"0\" />\r\n        </div>\r\n        <div class=\"ClsErrMsg\">\r\n            <span id=\"lblMessage\"></span>\r\n        </div>\r\n        <hr />\r\n      </form>\r\n   </body>\r\n</html>

Questions

  1. Even though network credentials are passed why are we getting a HTML response with the login page?

  2. What might cause the exception in in this line? - _xmlDocument = XElement.Load(streamResult)

  3. Are we missing anything in the way we are accessing the Azure hosted XML from C# code?

  4. Should we be rather looking at the server side for correcting the URL to the direct XML file based on what you can understand from the first four lines of our problem description?

Thanks in Advance,

Trilok Rangan.

Getting html back might be caused by running into a generic error handling page on the server when encountering an exception.

So make sure you return xml in all cases, even in case of an exception.

The exception can be caused by security issues or any other error on the server.

Make sure the content type that is set by the server is correct.

If the file is stored in blob-storage: mark the file's mime type in the blob storage explorer

If the file is static content: add the mime type in the web.config using the mimeMap element :

<configuration>

    <system.webServer>

        <staticContent>
            <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
            <mimeMap fileExtension=".m4v" mimeType="video/m4v" />
        </staticContent>

    </system.webServer>

</configuration> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM