我正在尝试从需要身份验证才能获取文件的 Azure SQL 报告服务器下载 xml 文件。我正在尝试以下两种方法:
方法一
在这种方法中,我尝试在 c# 控制台应用程序下面,它从服务器下载文件。它工作正常!
void DownloadFile(string uname, string password)
{
ServerReport report = new ServerReport();
report.ReportServerUrl = new System.Uri("https://xxxxx.reporting.windows.net/ReportServer", System.UriKind.Absolute);
report.ReportPath = "/Demo2.rdl";
report.ReportServerCredentials.SetFormsCredentials(null, uname, password, "xxxxx.reporting.windows.net");
byte[] data = report.Render("XML");
FileStream fs = new FileStream(@"c:\output.xml", FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
}
方法二
现在我想从 Windows Phone 应用程序下载相同的文件。现在,由于 Windows Phone 中不存在 ServerReport,我依靠 WebClient 来下载文件。所以我正在尝试下面的代码,但它似乎不起作用:
void DownloadFile(string uname, string password)
{
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(uname, password, "xxxxx.reporting.windows.net");
webClient.DownloadFile("https://xxxxx.reporting.windows.net/ReportServer?%2fDemo2.rdl&rs:Command=Render&rs:Format=XML", @"c:\output.xml");
}
下面是方法 2 的输出:看起来它正在重定向到登录页面?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html lang="en-US">
<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>
Windows Azure SQL Reporting
</title></head>
<body >
<form name="Logon" method="post" action="logon.aspx?ReturnUrl=%2fReportServer%3f%252fDemo2.rdl%26rs%3aCommand%3dRender%26rs%3aFormat%3dXML&%2fDemo2.rdl&rs%3aCommand=Render&rs%3aFormat=XML" id="Logon">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/XXXX" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/YYYY" />
<h1 id="Title"><span id="LblTitleContent">Windows Azure</span></h1>
<hr />
<div class="ClsIntro">
<span id="LblIntro"></span>
</div>
<div class="ClsInput">
<div>
<label for="TxtUser" id="LblUser">User name</label>
</div>
<div>
<input name="TxtUser" type="text" id="TxtUser" tabindex="1" />
</div>
</div>
<div class="ClsInput">
<div>
<label for="TxtPwd" id="LblPwd">Password</label>
</div>
<div>
<input name="TxtPwd" type="password" id="TxtPwd" tabindex="2" />
</div>
</div>
<div class="ClsSignIn">
<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" />
</div>
<div class="ClsErrMsg">
<span id="lblMessage"></span>
</div>
<hr />
</form>
</body>
</html>
方法2可能有什么问题,我们错过了什么吗?
更新
我对这个天蓝色和网络服务很陌生。我可能会问一些愚蠢的问题。这是我尝试过的:
- 我尝试在我的 windows phone 7 项目中向 azure sql 报告服务器添加“服务参考”。我注意到的一件事是我无法将服务添加为“Web 参考”,因为它在 Windows Phone 7 应用程序中被禁用。
- 将服务添加到我的应用程序后,我得到了一组课程。其中之一是 ReportExecutionServiceSoapClient;
- ReportExecutionService 不可用。
- 我不太确定如何使用 ReportExecutionServiceSoapClient api 来获取可用于 WebClient 的 cookie。
- 任何关于如何使用这些 API 的示例应用程序都会很棒。我尝试通过互联网搜索,但无法获得更多信息。
以下是我尝试过的,但 cookie 计数为空。
ReportExecutionServiceSoapClient client = new ReportExecutionServiceSoapClient(); void MainPage_Loaded(object sender, RoutedEventArgs e) { client.ClientCredentials.UserName.UserName = "XXXX"; client.ClientCredentials.UserName.Password = "XXXX"; client.CookieContainer = new CookieContainer(); client.LogonUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_LogonUserCompleted); client.LogonUserAsync("XXXX", "XXXX", "xxxx.reporting.windows.net"); } void client_LogonUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { int count = client.CookieContainer.Count; // NULL COOKIE COUNT!!! }
下面是 ClientConfig 文件。我添加了 enableHttpCookieContainer="true"。
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ReportExecutionServiceSoap" maxBufferSize="2147483647"
enableHttpCookieContainer="true"
maxReceivedMessageSize="2147483647">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://xxxx.reporting.windows.net:443/ReportServer/ReportExecution2005.asmx"
binding="basicHttpBinding" bindingConfiguration="ReportExecutionServiceSoap"
contract="SqlReportingService.ReportExecutionServiceSoap"
name="ReportExecutionServiceSoap" />
</client>
</system.serviceModel>
</configuration>
- 请让我知道缺少什么。
已解决 - 最后我能够从 Azure Sql 报告服务器下载 xml 文件。- 以下是解决方案。- 首先添加对Sql 报告服务器的服务引用。- 然后执行 ReportExecutionServiceSoapClient::LogonUserAsync。- 将获得的 cookie 与 WebClient 一起使用以进行进一步请求。
public class WebClientExtended : WebClient
{
private CookieContainer myContainer;
private HttpWebRequest myRequest;
[SecuritySafeCritical]
public WebClientExtended()
{
}
[SecuritySafeCritical]
public WebClientExtended(CookieContainer containter)
{
myContainer = containter;
}
public CookieContainer Cookies
{
get { return myContainer; }
set { myContainer = value; }
}
protected override WebRequest GetWebRequest(Uri address)
{
myRequest = (HttpWebRequest)base.GetWebRequest(address);
myRequest.Method = "GET";
myRequest.CookieContainer = Cookies;
return myRequest;
}
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
return myRequest.EndGetResponse(result);
}
}
ReportExecutionServiceSoapClient rs = new ReportExecutionServiceSoapClient();
string servername = "XXXX.reporting.windows.net";
string uname = "XXXX";
string password = "XXXX";
public void LoadReport()
{
rs.ClientCredentials.UserName.UserName = uname;
rs.ClientCredentials.UserName.Password = password;
rs.CookieContainer = new CookieContainer();
rs.LogonUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(rs_LogonUserCompleted);
rs.LogonUserAsync(uname, password, servername);
}
void rs_LogonUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
WebClientExtended webClient = new WebClientExtended(rs.CookieContainer);
webClient.Credentials = new NetworkCredential(uname, password, servername);
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(String.Format("https://{0}/ReportServer?%2fDemo2.rdl&rs:Command=Render&rs:Format=XML", servername)));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}