Hello, I have a weather alert ticker that gets information from the National Weather Service. They now require a User-Agent in order for programs to access the RSS Feed. Here is the code in my program that access the feed:
Code:
Option Strict On
Imports System.IO
Imports System.Xml
Imports System.Net
Public Class Form1
Dim XmlDoc As New XmlDocument()
Dim Bmp As Bitmap
Dim WXDict As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
TextBox2.Text = "http://alerts.weather.gov/cap/ks.atom"
Timer1.Interval = 1
Timer2.Interval = 10
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
ScrollText = False
PictureBox1.Refresh()
TextBox3.Clear()
TextBox3.Refresh()
Using g As Graphics = Graphics.FromHwnd(PictureBox1.Handle)
Dim SizeToUse = g.MeasureString(WXDict.Values(ListBox1.SelectedIndex), New Font("Cambria", 11))
Bmp = New Bitmap(CInt(SizeToUse.Width + 4), CInt(SizeToUse.Height + 4))
End Using
Using g As Graphics = Graphics.FromImage(Bmp)
g.DrawString(WXDict.Values(ListBox1.SelectedIndex), New Font("Cambria", 11), Brushes.Black, 2, 2)
End Using
x = PictureBox1.Width
PictureBox1.Height = Bmp.Height
TextBox3.Text = WXDict.Keys(ListBox1.SelectedIndex)
Timer2.Start()
End Sub
Dim x As Integer = 0
Dim ScrollText As Boolean = False
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If ScrollText = True Then
e.Graphics.DrawImage(Bmp, x, 0)
End If
End Sub
Dim LastWXData As String = ""
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Interval = 60000
Using WB As New WebClient
Try
Dim WXData As String = WB.DownloadString(TextBox2.Text)
If WXData <> LastWXData Then
LastWXData = WXData
ListBox1.Items.Clear()
ListBox1.Enabled = False
WXDict.Clear()
XmlDoc.LoadXml(WXData)
Dim TitleList As XmlNodeList = XmlDoc.GetElementsByTagName("title")
Dim SummaryList As XmlNodeList = XmlDoc.GetElementsByTagName("summary")
For i = 1 To TitleList.Count - 1
WXDict.Add(TitleList(i).InnerText, SummaryList(i - 1).InnerText)
Next
TextBox1.Text = TitleList(0).InnerText
TextBox1.Refresh()
ListBox1.Items.AddRange(WXDict.Keys.ToArray)
ListBox1.Enabled = True
ListBox1.SelectedIndex = 0
End If
Catch ex As Exception
End Try
End Using
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ScrollText = True
x -= 1
If x <= -Bmp.Width Then x = PictureBox1.Width
PictureBox1.Refresh()
End Sub
End Class
The User-Agent code I have found is in c#. Here it is:
Code:
var request = new HttpRequestMessage(HttpMethod.Post, httpClient.BaseAddress.AbsoluteUri);
request.Headers.Add("User-Agent", "MyApplication/v1.0 (http://foo.bar.baz; foo@bar.baz)");
var httpResponse = httpClient.SendAsync(request).Result;