1

I'm making a Windows Phone 8 app. I have the latitude and longitude of the target location. I need to get the Two Letter ISO country code of target location.

I'm using the following code to make it happen.

        'Dim address As String = puri
        'Dim client As WebClient = New WebClient()
        'Dim reader As StreamReader = New StreamReader(address)
        'code = reader.ReadToEnd
        Dim inStream As StreamReader
        Dim wr As WebRequest
        Dim webresponse As WebResponse
        wr = WebRequest.Create(puri)
        webresponse = wr.GetResponse()
        inStream = New StreamReader(webresponse.GetResponseStream())
        code = inStream.ReadToEnd()

where puri(in the commented code) is the address of the webservice in string format.

When trying the commented code, the error I'm getting is that string cannot be converted to system.uri format. (address)

When trying the uncommented code, I get an error which says, getresponse is not a member of class system.net.webrequest()

I guess with the updates to .NET the code changed, but I couldn't find anything current on the topic.

URI = http://api.geonames.org/countryCode?lat=17.60890&lng=76.98966&username=demo

1 Answer 1

-1

I think you should use WebClient Class instead of a WebRequest. It is simpler and faster. Here is a simple example:

Dim WebCL As New WebClient
Dim DownLoadedText As String = String.Empty
Try
    DownLoadedText = WebCL.DownloadString("Your Url")
    ' Do something 
Catch ex As Exception
    Throw New Exception("Oops!! ERROR has occured, something is wrong with your address")
End Try
3
  • DownloadString is not a member system.net.webclient. I found this code somewhere else. Didn't work because of this error.
    – doodla
    Sep 4, 2014 at 4:02
  • Actually it is....MSDN
    – Eminem
    Sep 4, 2014 at 12:23
  • It doesn't work. I know the documentation shows that DownloadString belongs to the webclient class. But intellisense shows only DownloadStringAsync for an object of WebClient. And just forcing DownloadString produces an error.
    – doodla
    Sep 4, 2014 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.