System.Net.WebException: 'The request failed with HTTP status 403: Forbidden.'

  • sgmunson

    SSC Guru

    Points: 110443

    When my application code goes to set the parameters for a reportviewer control for an SSRS report, I get the

    System.Net.WebException: 'The request failed with HTTP status 403: Forbidden.' error

    Not sure why.   I've set the credentials to the default network credentials, and I know have rights to see that report.   Wondering why I can't set the parameters.   EDIT: (My SSRS site does use a certificate, and that certificate is installed locally on my laptop... maybe this has something to do with it?).

    Here's the VB .Net code:

    Private Sub GetImpactButton_Click(sender As Object, e As EventArgs) Handles GetImpactButton.Click

    Dim VendorID As String, Vendor As String, strSQL As String, strPartsList As String, I As Integer, strNewPrices As String, strNewEffDates As String
    Dim strExcelRows As String
    VendorID = VendorID_TB.Text.ToString
    Vendor = VendorCB.Text.ToString
    strPartsList = ""
    strNewPrices = ""
    strNewEffDates = ""
    strExcelRows = ""
    I = 0
    For Each row As DataGridViewRow In DGV1.Rows
    I = I + 1
    If row.Cells(8).Value = 1 Then
    If (Not row.IsNewRow) And (Not row.Cells(0) Is DBNull.Value) Then
    strPartsList = strPartsList & row.Cells(3).Value.ToString & ","
    strNewPrices = strNewPrices & row.Cells(5).Value.ToString & ","
    strNewEffDates = strNewEffDates & row.Cells(4).Value.ToString & ","
    strExcelRows = strExcelRows & row.Cells(0).Value.ToString & ","
    End If
    End If
    Next
    strPartsList = Microsoft.VisualBasic.Left(strPartsList, Len(strPartsList) - 1) & "'"
    strNewPrices = Microsoft.VisualBasic.Left(strNewPrices, Len(strNewPrices) - 1) & "'"
    strNewEffDates = Microsoft.VisualBasic.Left(strNewEffDates, Len(strNewEffDates) - 1) & "'"
    strExcelRows = Microsoft.VisualBasic.Left(strExcelRows, Len(strExcelRows) - 1) & "';"

    Dim ErrorMessage As String
    'Set the processing mode for the ReportViewer to Remote
    RV1.ProcessingMode = ProcessingMode.Remote

    Dim serverReport As ServerReport
    serverReport = RV1.ServerReport

    'Get a reference to the default credentials
    Dim credentials As System.Net.ICredentials
    credentials = System.Net.CredentialCache.DefaultNetworkCredentials

    'Get a reference to the report server credentials
    Dim rsCredentials As ReportServerCredentials
    rsCredentials = serverReport.ReportServerCredentials

    'Set the credentials for the server report
    rsCredentials.NetworkCredentials = credentials

    'Set the report server URL and report path
    serverReport.ReportServerUrl = New Uri("https://myservername/reports/report/Reports")
    serverReport.ReportPath = "/SFT/PURCHASING/SFT Price Change Impact Analysis"

    'Create the VendorID report parameter
    Dim VendorIDParm As New ReportParameter()
    VendorIDParm.Name = "VendorID"
    VendorIDParm.Values.Add(VendorID)

    'Create the PartList report parameter
    Dim PartListParm As New ReportParameter()
    PartListParm.Name = "PartList"
    PartListParm.Values.Add(strPartsList)

    'Create the NewPriceList report parameter
    Dim NewPriceListParm As New ReportParameter()
    NewPriceListParm.Name = "NewPriceList"
    NewPriceListParm.Values.Add(strNewPrices)

    'Create the NewEffectiveDates report parameter
    Dim NewEffDatesParm As New ReportParameter()
    NewEffDatesParm.Name = "NewEffectiveDates"
    NewEffDatesParm.Values.Add(strNewEffDates)

    'Create the VendorID report parameter
    Dim ExcelRowsParm As New ReportParameter()
    ExcelRowsParm.Name = "NewEffectiveDates"
    ExcelRowsParm.Values.Add(strExcelRows)

    'Set the report parameters for the report
    Dim parmList As Generic.List(Of ReportParameter) = New Generic.List(Of ReportParameter)()
    parmList.Add(VendorIDParm)
    parmList.Add(PartListParm)
    parmList.Add(NewPriceListParm)
    parmList.Add(NewEffDatesParm)
    parmList.Add(ExcelRowsParm)
    RV1.ServerReport.SetParameters(parmlist)

    'Refresh the report
    RV1.RefreshReport()

    'Form2.Show()
    Exit Sub
    '... other commented out code here that you can never reach.
    End Sub

    • This topic was modified 6 months, 3 weeks ago by  sgmunson. Reason: Added information about the fact that my SSRS site uses a certificate which I have installed locally

    Steve
    ‌(aka sgmunson)
    ‌:) 🙂 🙂
    Health & Nutrition

  • sgmunson

    SSC Guru

    Points: 110443

    Well, folks, it seems I've had a "dumb" problem, in that the server URL was not set correctly, but I only discovered that after having to scour the web for a day and a half.   However, while I no longer get the 403 error, I am now in the position of not being able to SEE the reportviewer control at all.   I started out with that control's visibility property set to true, and it wouldn't show up at all, and then I changed it to false on the object property in the form and then set it True in VB code before the .Show method.   I still can't see the control.   Not sure how this is supposed to work, as it never appeared to occupy any space on the form itself... it just appears down below in Visual Studio.  I don't really have space on my form for it, but does it need any, or is it supposed to be a pop-up form of it's own accord?   Anyone?

    Steve
    ‌(aka sgmunson)
    ‌:) 🙂 🙂
    Health & Nutrition

  • sgmunson

    SSC Guru

    Points: 110443

    Here's the updated VB Code:

        Private Sub GetImpactButton_Click(sender As Object, e As EventArgs) Handles GetImpactButton.Click

    Dim VendorID As String, Vendor As String, strSQL As String, strPartsList As String, I As Integer, strNewPrices As String, strNewEffDates As String
    Dim strExcelRows As String
    VendorID = VendorID_TB.Text.ToString
    Vendor = VendorCB.Text.ToString
    strPartsList = ""
    strNewPrices = ""
    strNewEffDates = ""
    strExcelRows = ""
    I = 0
    For Each row As DataGridViewRow In DGV1.Rows
    I = I + 1
    If row.Cells(8).Value = 1 Then
    If (Not row.IsNewRow) And (Not row.Cells(0) Is DBNull.Value) Then
    strPartsList = strPartsList & row.Cells(3).Value.ToString & ","
    strNewPrices = strNewPrices & row.Cells(5).Value.ToString & ","
    strNewEffDates = strNewEffDates & row.Cells(4).Value.ToString & ","
    strExcelRows = strExcelRows & row.Cells(0).Value.ToString & ","
    End If
    End If
    Next
    strPartsList = Microsoft.VisualBasic.Left(strPartsList, Len(strPartsList) - 1) ' & "'"
    strNewPrices = Microsoft.VisualBasic.Left(strNewPrices, Len(strNewPrices) - 1) ' & "'"
    strNewEffDates = Microsoft.VisualBasic.Left(strNewEffDates, Len(strNewEffDates) - 1) ' & "'"
    strExcelRows = Microsoft.VisualBasic.Left(strExcelRows, Len(strExcelRows) - 1) ' & "';"

    Dim ErrorMessage As String

    'Set the processing mode for the ReportViewer to Remote
    RV1.ProcessingMode = ProcessingMode.Remote

    Dim serverReport As ServerReport
    serverReport = RV1.ServerReport

    'Get a reference to the default credentials
    Dim credentials As System.Net.ICredentials
    credentials = System.Net.CredentialCache.DefaultNetworkCredentials

    'Get a reference to the report server credentials
    Dim rsCredentials As ReportServerCredentials
    rsCredentials = serverReport.ReportServerCredentials

    'Set the credentials for the server report
    rsCredentials.NetworkCredentials = credentials

    'Set the report server URL and report path
    serverReport.ReportServerUrl = New Uri("https://myservername/reportserver")
    serverReport.ReportPath = "/Reports/SFT/PURCHASING/SFT Price Change Impact Analysis"

    'Create the VendorID report parameter
    Dim VendorIDParm As New ReportParameter()
    VendorIDParm.Name = "VendorID"
    VendorIDParm.Values.Add(VendorID)

    'Create the PartList report parameter
    Dim PartListParm As New ReportParameter()
    PartListParm.Name = "PartList"
    PartListParm.Values.Add(strPartsList)

    'Create the NewPriceList report parameter
    Dim NewPriceListParm As New ReportParameter()
    NewPriceListParm.Name = "NewPriceList"
    NewPriceListParm.Values.Add(strNewPrices)

    'Create the NewEffectiveDates report parameter
    Dim NewEffDatesParm As New ReportParameter()
    NewEffDatesParm.Name = "NewEffectiveDates"
    NewEffDatesParm.Values.Add(strNewEffDates)

    'Create the VendorID report parameter
    Dim ExcelRowsParm As New ReportParameter()
    ExcelRowsParm.Name = "ExcelRows"
    ExcelRowsParm.Values.Add(strExcelRows)
    'RV1.ServerReport.BearerToken

    'Set the report parameters for the report
    Dim parmList As Generic.List(Of ReportParameter) = New Generic.List(Of ReportParameter)()
    parmList.Add(VendorIDParm)
    parmList.Add(PartListParm)
    parmList.Add(NewPriceListParm)
    parmList.Add(NewEffDatesParm)
    parmList.Add(ExcelRowsParm)
    RV1.ServerReport.SetParameters(parmlist)

    'Refresh the report
    RV1.AutoSize = True
    RV1.Visible = True
    RV1.Show()
    RV1.RefreshReport()

    Exit Sub

    End Sub

    Steve
    ‌(aka sgmunson)
    ‌:) 🙂 🙂
    Health & Nutrition

  • sgmunson

    SSC Guru

    Points: 110443

    Well, this has been an interesting exercise...   I've learned a couple of things, and hope some of this is useful, as my troubles are not yet over.   I'm communicating with someone at Microsoft at this point.   Here's what I know so far:

    1.) Learned  I had to actually Add the control to the form using code.   That at least allows the control to appear.

    2.) Waste of time to set the location in the control properties, as it defaults to 0,0 and even if you set it otherwise, it ends up there.   I had to use code to set the location to a New System.Drawing.Point(x, y).

    3.) The control now appears, but the only parts visible are the control strip (which is greyed out, but does allow you to "hover" and see what each item is), and the outline (border).  No report or anything else.

    4.) A new NuGet package for the control was just released this week, so I downloaded the new package, and had to re-boot my Windows 7 machine to get things to work, after having uninstalled the old and installed the new.   This is the package for the ReportViewer control.

    5.) Some projects sure can be a hot mess...

    I'll update again and lay out the whole process from beginning to end when I finally get resolution.

    Steve
    ‌(aka sgmunson)
    ‌:) 🙂 🙂
    Health & Nutrition

  • sgmunson

    SSC Guru

    Points: 110443

    Okay, folks... I finally have this working.   I was actually able to demonstrate the working report today and I'm a very happy camper.   It's been a rather long hard road to get there.   Here's what it took for me:

    1.) Visual Studio 2017 CE (community edition) v15.9.14

    2.) SSDT installed into that instance of VS 2017 CE.

    3.) VSIX package for the MS RDLC designer installed into VS 2017 CE.

    4.) Add the v150.1538.0 of the NuGet package for the ReportViewer control to the VS 2017 CE installation using the NuGet Package Manager

    5.) Ensure that the Report Server url points to the http://servername/ReportServer link, and not to the link to the reports home page.  This is what got me the security error in the first place.

    5.) In the Form Load event, you actually have to add the report viewer control to the form to have it appear, as the design surface only shows the control appearing in the strip below where things like data adapters go.

    6.) Use a string array of type ReportParameter to hold the parameter values.

    7.) Do NOT use ReportViewer1.ReportServer.Refresh().  Instead, use ReportViewer1.RefreshReport().

    This was only accomplished today, after having a quick phone call with Brad Supyta of Microsoft, who was able to get me the right method (number 7 above).   Without that, I'd still be dead in the water, as the online resources for this are kind of scattered and inconsistent, and much of it is in the "ancient history" category.   Heavy duty kudos to Brad and his quick (30 seconds) assistance.

    I've posted these all in the hope that someone else doesn't  have to struggle quite so much.   This was something I had done previously back in the .Net 4.1 days and several SQL Server versions ago, so I had really thought it would be similarly easy.   Unfortunately, what's out there in the online documentation is nowhere near as good as it once was (don't remind me of the song about that - I already am singin' it...).

    Steve
    ‌(aka sgmunson)
    ‌:) 🙂 🙂
    Health & Nutrition

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply