2013-04-04 18 views
0
Public Class Geocode 
Public Structure GeocodeResult 
    Public Latitude As String 
    Public Longitude As String 
    Public Result As String 
End Structure 


Public Shared Function GetGeocode(ByVal Address As String) As GeocodeResult 
    Dim strLat As String = "" 
    Dim strLon As String = "" 
    Dim strResult As String = "" 
    Dim oXmlDoc As Object 
    GetGeocode.Latitude = "" 
    GetGeocode.Longitude = "" 
    GetGeocode.Result = "" 
    Try 
     Dim baseURL As String = "https://maps.google.com/maps/api/geocode/xml?sensor=false&address=" & Address 
     baseURL = Replace(baseURL, " ", "+") 
     oXmlDoc = CreateObject("Microsoft.XMLDOM") 
     With oXmlDoc 
      .Async = False 
      If .Load(baseURL) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then 
       GetGeocode.Result = .selectSingleNode("GeocodeResponse/status").Text 
       If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then 
        GetGeocode.Latitude = .selectSingleNode("//location/lat").Text 
        GetGeocode.Longitude = .selectSingleNode("//location/lng").Text 
        Return GetGeocode 
       End If 
      End If 
     End With 
     oXmlDoc = Nothing 
     Return GetGeocode 
     Exit Function 
    Catch ex As Exception 
     Throw (ex) 
    End Try 
    Return GetGeocode 
End Function 
End Class 

好的,這樣它可以在生產,qa和localhost中正常工作,直到我們將它移動到Azure虛擬機。在VM中,我們可以使用瀏覽器訪問https://maps.google.com/maps/api/geocode/xml?sensor=false&address=網址。但是當另一個頁面調用getgeocode函數時,結果始終爲空,意味着其他api調用以某種方式失敗。無法從Azure虛擬機調用Google API

我不認爲它的域密鑰限制,因爲a)即時通訊不使用在這個調用中的關鍵和b)我設置我的谷歌API密鑰到任何域來測試它。編輯:我試過用相同的結果使用另一項服務。它適用於開發人員和本地機器,但不適用於Azure虛擬機。我不明白的是如何通過瀏覽器訪問REST apis,但從代碼調用時不返回任何內容。 有什麼建議嗎?

回答

0

顯式創建一個Web客戶端並將其加載到xml文檔中已解決此問題。

Imports Microsoft.VisualBasic 
Imports System.Xml 
Imports System.Linq 
Imports System.Xml.Linq 
Imports System.Net 
Imports System.IO 

Public Class Geocode 
    Public Structure GeocodeResult 
     Public Latitude As String 
     Public Longitude As String 
     Public Result As String 
    End Structure 


    Public Shared Function GetGeocode(ByVal Address As String) As GeocodeResult 
     Dim oXmlDoc As Object 
     GetGeocode.Latitude = "" 
     GetGeocode.Longitude = "" 
     GetGeocode.Result = "" 
     Try 
      Dim baseURL As String = "https://maps.google.com/maps/api/geocode/xml?sensor=false&address=" & Address 
      baseURL = Replace(baseURL, " ", "+") 
      Using WC As New WebClient() 
       oXmlDoc = CreateObject("Microsoft.XMLDOM") 
       With oXmlDoc 
        .Async = False 
        If .Load(WC.DownloadData(baseURL)) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then 
         GetGeocode.Result = .selectSingleNode("GeocodeResponse/status").Text 
         If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then 
          GetGeocode.Latitude = .selectSingleNode("//location/lat").Text 
          GetGeocode.Longitude = .selectSingleNode("//location/lng").Text 
          Return GetGeocode 
         End If 
        End If 
       End With 
       oXmlDoc = Nothing 
      End Using 

      Return GetGeocode 
      Exit Function 
     Catch ex As Exception 
      Throw (ex) 
     End Try 
     Return GetGeocode 
    End Function 
End Class