28,655 questions
VB.NET Winform HttpWebRequest / XmlDocument Error: The remote server returned an error: (403) Forbidden.
jim brown
271
Reputation points
So the below code works on my home network but at work my .NET application gets a 403 Forbidden. I believe ZScaler firewall is blocking traffic, but I can view XML via browser without issue just not in a .NET application. I have read that it's trying to block BOT traffic so adding a .UserAgent should fix the issue, It did not.
I am just trying to read an XLM file from the internet and can view from a webrowser.
What alternatives are there for this issue?
Try
Dim url As String = "https://www.espn.com/espn/rss/news"
Dim rssFeed As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
'rssFeed.Method = "POST"
'rssFeed.Accept = "application/xml"
'rssFeed.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
'rssFeed.Credentials = CredentialCache.DefaultNetworkCredentials
'rssFeed.Proxy.Credentials = CredentialCache.DefaultCredentials
Dim response = rssFeed.GetResponse()
Dim rssStream = response.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)
Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
Dim i As Integer = 0
Dim dt As DataTable = New DataTable("table")
dt.Columns.Add("Title", Type.GetType("System.String"))
dt.Columns.Add("Description", Type.GetType("System.String"))
dt.Columns.Add("Link", Type.GetType("System.String"))
While i < rssItems.Count
Dim node As XmlNode = rssItems.Item(i).SelectSingleNode("title")
Dim title As String
Dim description As String
Dim link As String
If node IsNot Nothing Then
title = node.InnerText
Else
title = ""
End If
node = rssItems.Item(i).SelectSingleNode("description")
If node IsNot Nothing Then
description = node.InnerText
Else
description = ""
End If
node = rssItems.Item(i).SelectSingleNode("link")
If node IsNot Nothing Then
link = node.InnerText
Else
link = ""
End If
Dim dr As DataRow = dt.NewRow()
dr("Title") = title
dr("Description") = description
dr("Link") = link
dt.Rows.Add(dr)
i += 1
End While
DataGridView1.DataSource = dt
WebBrowser1.ScriptErrorsSuppressed() = True
WebBrowser1.Navigate("https://www.espn.com/espn/rss/news")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | .NET | Other
4,103 questions
Developer technologies | VB
2,892 questions
Developer technologies | Visual Studio | Other
5,451 questions
Sign in to answer