Simple web request

This should be simple, I say someone do it before... from a vb.net app request from a weather service web site, the current temperature and read that (xml) and display it in the text box....

Don't know if I have any of my code correct, that is why I didn't post it.. I get an error also saying that

"The remote server returned an error: (407) Proxy Authentication Required."

Can someone help me....

[456 byte] By [BrianShafer] at [2007-12-24]
# 1
Can you post the code you're using to download from the web site and the URL of the site?
JaredParsonsMSFT at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

If you dont know that you have any code correct and you arent showing us any code - how are we really meant to tell you why its not working. You obviously have some specific web service you are trying to communicate with - provide us with the details and show us a the code snippet. It cant be that much code.

Then we will be able to assist more - dont be shy, we are here to help you.

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

you can just copy all this into a form....

Imports System.Xml

Imports System.Xml.Serialization

Imports System.Net.WebClient

Imports System.Net.WebProxy

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim ds As New DataSet

Dim myadd As Uri = New Uri("Http://isa:80")

Me.txtZip.Text = "41018"

Dim strUrl As String = String.Format("http://xml.weather.yahoo.com/forecastrss?p={0}", txtZip.Text)

Dim nc As New Net.NetworkCredential("username", "password", "domain")

Dim wp As New Net.WebProxy

Dim wr As Net.WebRequest = Net.WebRequest.Create("http://forums.microsoft.com/MSDN/rss.aspx?ForumID=159&Mode=0&SiteID=1")

' set default credentials

'.GetProxy(http://isa:80 )

wp.Credentials = nc 'System.Net.CredentialCache.DefaultCredentials

wp.Address = myadd

'wp.Credentials = System.Net.CredentialCache.DefaultCredentials

Try

ds.ReadXml("http://forums.microsoft.com/MSDN/rss.aspx?ForumID=159&Mode=0&SiteID=1")

DataGridView1.DataSource = ds.Tables("item")

Catch ex As Exception

MessageBox.Show(ex.ToString)

End Try

''' This is what I thought I saw someone do, and it worked then.. guess they were not behind a proxy 'maybe?

'Dim weather As New XmlDocument

'weather.Load("http://www.noaa/gov/weather.xml")

'Dim currentweather As String = weather.SelectSingleNode("//temp_f").Value

'Me.TextBox1.Text = currentweather

''' End of what I was trying to do...

End Sub

Private Sub btnGetWeather_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetWeather.Click

Dim sr As IO.StreamReader

Dim wc As New System.Net.WebClient

Dim strUrl As String

Dim strOut As String = ""

'strUrl = String.Format("http://xml.weather.yahoo.com/forecastrss?p={0}", txtZip.Text)

strUrl = String.Format("http://rss.weather.com/weather/rss/local/40245?cm_ven=LWO&cm_cat=rss&par=LWO_rss", txtZip.Text)

Try

sr = New IO.StreamReader(wc.OpenRead(strUrl))

Dim strHtml As String = sr.ReadToEnd

Dim x As Integer = strHtml.IndexOf("<img src")

Dim y As Integer = strHtml.IndexOf(")<br/>")

strOut = strHtml.Substring(x, y - x + 6)

sr.Close()

Catch

strOut = "<h1>Error getting weather</h1>"

Finally

Me.TextBox1.Text = strOut

'WebBrowser1.DocumentText = strOut

End Try

End Sub

End Class

BrianShafer at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

The first code works just fine....

The second code for the weather breaks on the following lines

Dim y As Integer = strHtml.IndexOf(")<br/>")
strOut = strHtml.Substring(x, y - x + 6)

the <br/> is not found and y is then -1 which the results in the second call failing as the substring arguments are not.

You may want to add some additional code to allow better reporting of exceptions for debugging purposes.

Your original problem looks like its to do with the security on your network as the code appears to get the XML without a problem here.

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...