HTTP Get to Report Server 2008 работает с DefaultCredentials, не работает с некоторыми NetworkCredentials - PullRequest
0 голосов
/ 24 ноября 2010

Следующее WithDefaultCredentials () работает, но WithCredentialsMe () завершается неудачно с возвращенным http 401? Разница в том, что Учетные данные ICredentials = System.Net.CredentialCache.DefaultCredentials; работает нормально с URL сервера отчетов 2008, но ICredentials credentials = new NetworkCredential ("myUsername", "myPassword", "ourDomain"); возвращается с HTTP 401.

Консольное приложение разрабатывается мной, поэтому не должно быть разницы между DefaultCredentials и NetworkCredential. Там нет проблем с моим именем пользователя и паролем.

Есть идеи?

static void Main(string[] args)
{
    WithDefaultCredentials();
    WithCredentialsMe();
}
public static void WithDefaultCredentials()
{
    try
    {
        ICredentials credentials = System.Net.CredentialCache.DefaultCredentials;
        string url = "http://myBox/ReportServer_SQLSERVER2008/Pages/ReportViewer.aspx?%2fElfInvoice%2fElfInvoice&rs:Command=Render&InvoiceID=115abba9-61bb-4070-bd28-f572115a2860&rs:format=PDF";
        var bytes = GetByteListFromUrl(url, credentials);
        File.WriteAllBytes(@"c:\temp\A_WithDefaultCredentitials.pdf", bytes.ToArray());
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
public static void WithCredentialsMe()
{
    try
    {
        ICredentials credentials = new NetworkCredential("myUsername", "myPassword", "ourDomain");
        string url = "http://myBox/ReportServer_SQLSERVER2008/Pages/ReportViewer.aspx?%2fElfInvoice%2fElfInvoice&rs:Command=Render&InvoiceID=115abba9-61bb-4070-bd28-f572115a2860&rs:format=PDF";
        var bytes = GetByteListFromUrl(url, credentials);
        File.WriteAllBytes(@"c:\temp\A_Credentials_me_1.pdf", bytes.ToArray());
    }
    catch( Exception ex )
    {
        Console.WriteLine( ex.Message);
    }
}
public static List<Byte> GetByteListFromUrl(string url, System.Net.ICredentials credentials)
{
    List<Byte> lstByte = new List<byte>();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    if (credentials != null)
    {
        request.Credentials = credentials;
    }
    var response = (HttpWebResponse)request.GetResponse();
    var stream = response.GetResponseStream();
    int totalBytesRead = 0;
    int bufferbytesRead = 0;
    try
    {
        byte[] buffer = new byte[1024];
        while ((bufferbytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            totalBytesRead += bufferbytesRead;
            if (bufferbytesRead < buffer.Length)
            {
                bufferbytesRead = bufferbytesRead - 1 + 1;
            }
            for (int i = 0; i < bufferbytesRead; i++)
            {
                var bToAdd = buffer[i];
                lstByte.Add(bToAdd);
            }
        }
    }
    catch (Exception ex)
    {

    }
    finally{}
    //-Return 
    return lstByte;
}

1 Ответ

0 голосов
/ 25 ноября 2010

С помощью http://forums.asp.net/t/1217642.aspx этот код дал мне то, что я хотел ... Следующий шаг - очистить все это и выполнить модульное тестирование в dev ...

    public static void ReportServerWebService()
    {
        // wsdl /out:rs.cs /namespace:ReportService2005 http://mybox/ReportServer_SQLSERVER2008/ReportService2005.asmx?wsdl 
        /// wsdl /out:rsExec.cs /namespace:ReportExecution2005  http://mybox/ReportServer_SQLSERVER2008/ReportExecution2005.asmx?wsdl
        ICredentials credentials = new NetworkCredential("myUserName", "myPassword", "hcml");
        Guid invoiceID = new Guid("115ABBA9-61BB-4070-BD28-F572115A2860"); 
        var rs = new ReportService2005.ReportingService2005();
        var rsExec = new ReportExecution2005.ReportExecutionService();
        rs.Credentials = credentials;
        rsExec.Credentials = credentials;
        string historyID = null;
        string deviceInfo = null;
        string format = "PDF";
        Byte[] bytPDF;
        string encoding = String.Empty;
        string mimeType = String.Empty;
        string extension = String.Empty;
        ReportExecution2005.Warning[] warnings = null;

        string[] streamIDs = null;
        string _reportName = "/ElfInvoice/ElfInvoice" ;
        string _historyID = null;
        bool _forRendering = false;
        ReportService2005.ParameterValue[] _values = null;
        ReportService2005.DataSourceCredentials[] _credentials = null;
        ReportService2005.ReportParameter[] _parameters = null;
        try
        {
            // Get if any parameters needed.
            _parameters = rs.GetReportParameters( _reportName, _historyID, _forRendering, _values, _credentials);
            // Load the selected report.
            var ei = rsExec.LoadReport(_reportName, historyID);

            // Prepare report parameter.
            // Set the parameters for the report needed.
            var parameters = new ReportExecution2005.ParameterValue[1];

            //    // Place to include the parameter.
            if (_parameters.Length > 0)
            {
                parameters[0] = new ReportExecution2005.ParameterValue();
                parameters[0].Label = "InvoiceID";
                parameters[0].Name = "InvoiceID";
                parameters[0].Value = invoiceID.ToString();
            }
            rsExec.SetExecutionParameters(parameters, "en-us");
            bytPDF = rsExec.Render( format , deviceInfo , out extension , out encoding , out mimeType , out warnings , out streamIDs ) ;
            try
            {
                File.WriteAllBytes(@"c:\temp\A_WithMyCredentitials_ReportServerWebService.pdf", bytPDF.ToArray());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
...