Problems getting httpWebRequest to work
-
HI - and Help ! I have a small working app that formats a request to Google Maps, an retireves the data. I copied the code to access a USGS site - and the string works but the code hangs. Here is the code Sub GetElevationData() 'This is based upon GoogleMaps access code 'WebRequestString = "http://ws.geonames.org/gtopo30?lat=" & lineArray(LatPosition) & "&lng=" & lineArray(LonPosition) ' this is the eventual string . . . but I am using the following as a test - I can ctrl click and the link comes up ok with a 2 digit elevation. Dim test1 As String = "http://ws.geonames.org/gtopo30?lat=33.7669399&lng=-116.4134417" ' documentation is available at http://www.geonames.org" Dim Request As HttpWebRequest = CType(WebRequest.Create(test1), HttpWebRequest) 'Request.MaximumAutomaticRedirections = 5 Tried these (and lots other options - but no joy) 'Request.AllowAutoRedirect = True Dim Response As HttpWebResponse = CType(Request.GetResponse(), HttpWebResponse) 'This seems to time out Dim str As Stream = Response.GetResponseStream() Dim inBuf(10000) As Byte Dim bytesToRead As Integer = CInt(inBuf.Length) Dim bytesRead As Integer = 0 While bytesToRead > 0 Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead) If n = 0 Then Exit While End If bytesRead += n bytesToRead -= n End While MsgBox("bytesread " & bytesRead) 'says 4 'inBuf now has the elevation 'bytesRead has the string length Dim elevationvalue As String = inBuf.ToString MsgBox(elevationvalue) ' says system byte [] 'lineArray(elevationPosition) = elevationvalue Response.GetResponseStream().Dispose() End Sub Thanks Bob
-
HI - and Help ! I have a small working app that formats a request to Google Maps, an retireves the data. I copied the code to access a USGS site - and the string works but the code hangs. Here is the code Sub GetElevationData() 'This is based upon GoogleMaps access code 'WebRequestString = "http://ws.geonames.org/gtopo30?lat=" & lineArray(LatPosition) & "&lng=" & lineArray(LonPosition) ' this is the eventual string . . . but I am using the following as a test - I can ctrl click and the link comes up ok with a 2 digit elevation. Dim test1 As String = "http://ws.geonames.org/gtopo30?lat=33.7669399&lng=-116.4134417" ' documentation is available at http://www.geonames.org" Dim Request As HttpWebRequest = CType(WebRequest.Create(test1), HttpWebRequest) 'Request.MaximumAutomaticRedirections = 5 Tried these (and lots other options - but no joy) 'Request.AllowAutoRedirect = True Dim Response As HttpWebResponse = CType(Request.GetResponse(), HttpWebResponse) 'This seems to time out Dim str As Stream = Response.GetResponseStream() Dim inBuf(10000) As Byte Dim bytesToRead As Integer = CInt(inBuf.Length) Dim bytesRead As Integer = 0 While bytesToRead > 0 Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead) If n = 0 Then Exit While End If bytesRead += n bytesToRead -= n End While MsgBox("bytesread " & bytesRead) 'says 4 'inBuf now has the elevation 'bytesRead has the string length Dim elevationvalue As String = inBuf.ToString MsgBox(elevationvalue) ' says system byte [] 'lineArray(elevationPosition) = elevationvalue Response.GetResponseStream().Dispose() End Sub Thanks Bob
Just a couple of questions off the top of my head... Hangs where?? Which line?? Does it hang for a time, then resume?? Does it hang indefinately?? What's the size of the file it's trying to retrieve?? Is there network activity during the hang??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Just a couple of questions off the top of my head... Hangs where?? Which line?? Does it hang for a time, then resume?? Does it hang indefinately?? What's the size of the file it's trying to retrieve?? Is there network activity during the hang??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008HI - and thanks for the reply If you want to chat about this - call me at 510 889 1453 I Posted this reply a bit early - and have found a small glitch I have modified the code a bit - here is the most recent version using a directcast . . . The other version hung on the response statement - forever - and the network is basically empty during the hang The reply is a 1-4 byte string. ---> Glitch - the directcast returns the entire web page string - I found the following as an alternate suggestion - it works - but seems to time out on each request (my test file has 2 records, and the total time is about 12 seconds) - but if I un-comment the test1 string I get sub second response to a browser window. My DevEnv is VisualStudio2008 WebRequestString = "http://ws.geonames.org/gtopo30?lat=" & lineArray(LatPosition) & "&lng=" & lineArray(LonPosition) 'Dim test1 As String = "http://www.geonames.org/gtopo30?lat=33.7669399&lng=-116.4134417" Dim Request As HttpWebRequest Dim Response As HttpWebResponse Dim reader As StreamReader Dim sbSource As StringBuilder Try Request = DirectCast(WebRequest.Create(WebRequestString), HttpWebRequest) Request.KeepAlive = False response = DirectCast(Request.GetResponse(), HttpWebResponse) If Request.HaveResponse = True AndAlso Not (response Is Nothing) Then ' Get the response stream reader = New StreamReader(response.GetResponseStream()) ' Read it into a StringBuilder sbSource = New StringBuilder(reader.ReadToEnd()) End If Catch wex As WebException ' This exception will be raised if the server didn't return 200 - OK ' Try to retrieve more information about the network error If Not wex.Response Is Nothing Then Dim errorResponse As HttpWebResponse = Nothing Try errorResponse = DirectCast(wex.Response, HttpWebResponse) MsgBox("The server returned '{0}' with the status code {1} ({2:d})." & errorResponse.StatusDescription & " , " & errorResponse.StatusCode & " , " & errorResponse.StatusCode) Finally If Not errorResponse Is Nothing Then errorResponse.Close() End Try End If Finally If Not Response Is Nothing Then Response.Close() End Try