如何动态更改Web服务URL

时间:2011-09-20 10:47:13

标签: vb.net

我的项目中有一个Web服务引用,但它有两个url,一个是live,第二个是test,如何在vb.net中动态切换这些url

http://api.test/test/SOAP.wsdl
http://api.live/live/SOAP.wsdl

        'LOGPOINT:
        Call mobjLogWrite.prWriteLogEntry(clsLogWriter.enuLogEntryType.INFORMATION, ASSEMBLY_ID, "Start fnHOTELSPROSearchExecute()", "fnHOTELSPROSearchExecute")

        Dim objsoap As New b2bHotelSOAPService()
        Dim getres As New getAvailableHotelResponse()
        QLSearchXML = xmlData
        objsoap.Timeout = 20000
        objsoap.Url = "http://api.live/live/SOAP.wsdl"
        'objsoap.Timeout = TIMEOUT
        getres = objsoap.getAvailableHotel(HOTELSPRO_APIKEY.Trim(), strDestinationId, dtmCheckIn, dtmCheckOut, strCurrencyCode, "UK", True, fngetpax(xmlData), getfilter())
        Call mobjLogWrite.prWriteLogEntry(clsLogWriter.enuLogEntryType.INFORMATION, ASSEMBLY_ID, "Start DeSerializing the XML Output", "fnHOTELSPROSearchExecute")
        lHOTELSPROReturn = fnCustomSerializeObject(GetType(getAvailableHotelResponse), getres)
        Call mobjLogWrite.prWriteLogEntry(clsLogWriter.enuLogEntryType.INFORMATION, ASSEMBLY_ID, "End DeSerializing the XML Output", "fnHOTELSPROSearchExecute")
        lTempDOM.LoadXml(lHOTELSPROReturn)
        Return lTempDOM

    Catch ex As Exception
        Call mobjLogWrite.prWriteLogEntry(clsLogWriter.enuLogEntryType.ERROR, ASSEMBLY_ID, "Catch Block Error:" + ex.ToString(), "fnCreateHOTELSPROSearchRequest")

    Finally
        'LOGPOINT:
        Call mobjLogWrite.prWriteLogEntry(clsLogWriter.enuLogEntryType.INFORMATION, ASSEMBLY_ID, "Response From HotelsPro--->" & lHOTELSPROReturn, "fnHOTELSPROSearchExecute")
        Call mobjLogWrite.prWriteLogEntry(clsLogWriter.enuLogEntryType.INFORMATION, ASSEMBLY_ID, "END Finally Block fnHOTELSPROSearchExecute()", "fnHOTELSPROSearchExecute")
    End Try

返回错误响应

“我的项目中有一个Web服务引用,但它有两个url,一个是live,第二个是test,如何在vb.net中动态切换这些url”

2 个答案:

答案 0 :(得分:1)

动态地基于什么,确切地说?

假设您的意思是基于应用程序的运行位置,即测试或实时,如何:

编辑:刚刚看到它本来就是在VB.Net

Dim MyService as String
If HttpContext.Current.Server.MachineName.ToString() = "LIVESERVER" Then
    MyService = "http://api.live/live/SOAP.wsdl" 
Else
     MyService = "http://api.live/test/SOAP.wsdl"
End If

并改变

objsoap.Url = "http://api.live/live/SOAP.wsdl"

objsoap.Url = MyService

答案 1 :(得分:0)

如果您的Webservice设置为Dynamic,则URL将存储在app.config设置中。为了在运行时更容易更改(除非以管理员权限运行,否则app.config是只读的),转到项目设置并将webservice设置从应用程序范围更改为用户范围。

现在,您可以使用my.settings.yourwebserviceurl ... =“newwebserviceurl”

随时更改代码中的Web服务URL

下次调用Web服务时,它将来自新位置。但是,您需要确保两个webservice调用都包含一个conntical或至少兼容的Web服务。