Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problems getting httpWebRequest to work

Problems getting httpWebRequest to work

Scheduled Pinned Locked Moved Visual Basic
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rfrank5356
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • R rfrank5356

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        R Offline
        R Offline
        rfrank5356
        wrote on last edited by
        #3

        HI - 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups