欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vb 调用webservice 博客分类: 其他  

程序员文章站 2024-03-22 20:22:04
...
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json.Linq.JArray
Imports System.Web





' 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class HotInfoWs
    Inherits System.Web.Services.WebService
    Private BackendAddress As String = "http://172.17.16.55:8080"
    Private contentTypeHotDeals As String = "/cms/hotDeals/index.jhtml"
    Private contentTypeDealsForMembers As String = "/cms/dealsForMembers/index.jhtml"
    Dim noticeInfo As String = "please check the back network!"
    Dim strReturn As String = ""
    Public hh As New Hashtable
    Private deals = "deals"
    Private members = "members"
    '  Public request As System.Net.HttpWebRequest
    'Dim request1 As System.Web.HttpRequest



    <WebMethod()> _
   Public Function GetHotDeals() As String


        Return getMembersServiceContent(contentTypeHotDeals, deals)
    End Function

    <WebMethod()> _
    Public Function GetDealsForMembers() As String



        Return getMembersServiceContent(contentTypeDealsForMembers, members)
    End Function




    Private Function getMembersServiceContent(ByVal serviceType As String, ByVal functionType As String)



        Dim sm = Server.MapPath("~/") + "images\" + functionType + "\"
        Try
            Dim uri As New Uri(BackendAddress + serviceType)
            Dim request As HttpWebRequest = WebRequest.Create(uri)

            request.Method = "Get"
            request.Timeout = 60000
            Dim webrep As WebResponse = request.GetResponse()
            Dim resStream As Stream = webrep.GetResponseStream()

            Dim streamReader As New StreamReader(resStream, System.Text.Encoding.UTF8)
            strReturn = streamReader.ReadToEnd()
            streamReader.Close()
            resStream.Close()
            webrep.Close()
        Catch exxx As Exception
            Return noticeInfo
        End Try




        Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer
        Dim product As New ProductInfo
        '  Dim products As New JavaScriptSerializer

        ' JsonSerializer.Deserialize(new JsonTextReader(new StringReader(sw.GetStringBuilder().ToString())), typeof(ProductCollection));
        Dim dict = serializer.Deserialize(Of List(Of ProductInfo))(strReturn)
        Dim productNum = dict.Count

        Try
            Dim ht As Hashtable = Application(functionType)
            Dim list As New List(Of ProductInfo)
            If ht.Keys.Count = productNum Then
                Dim imgPath = ""
                For Each entry As DictionaryEntry In ht
                    Dim pro As ProductInfo = entry.Value
                    imgPath = "/images/" + functionType + "/" + Split(pro.imgPath, "/")(6)

                    pro.imgPath = imgPath

                    list.Add(pro)

                Next
                Dim str = JavaScriptConvert.SerializeObject(list)

                Return str

            Else

                Dim b() As Byte
                ' MsgBox(dict.First.title)
                For Each pro As ProductInfo In dict
                    Dim ss = BackendAddress + pro.imgPath
                    Dim imageName = Split(pro.imgPath, "/")(6)


                    Dim dFile As New System.Net.WebClient
                    Dim name = sm + imageName
                    Try

                        b = dFile.DownloadData(ss)

                        System.IO.File.WriteAllBytes(name, b)

                        pro.imgPath = "images/" + functionType + "/" + imageName

                        ' list1.Add(pro)


                    Catch exx As Exception



                    End Try

                    hh.Add(pro.author, pro)



                Next


                Application.Lock()

                Application(functionType) = hh
                Application.UnLock()




                Dim str = JavaScriptConvert.SerializeObject(dict)

                Return str



            End If

            '  MsgBox(ht.Keys.Count)
        Catch ex As Exception
            Application(functionType) = hh

            Dim b() As Byte
            ' MsgBox(dict.First.title)
            For Each pro As ProductInfo In dict
                Dim ss = BackendAddress + pro.imgPath
                Dim imageName = Split(pro.imgPath, "/")(6)


                Dim dFile As New System.Net.WebClient
                Dim name = sm + imageName
                Try

                    b = dFile.DownloadData(ss)

                    System.IO.File.WriteAllBytes(name, b)

                    pro.imgPath = "images/" + functionType + "/" + imageName

                    ' list1.Add(pro)


                Catch exx As Exception



                End Try

                hh.Add(pro.author, pro)



            Next


            Application.Lock()

            Application(functionType) = hh
            Application.UnLock()




            Dim str = JavaScriptConvert.SerializeObject(dict)

            Return str



        End Try





    End Function
    Public Class ProductInfo
        Public author As Integer
        Public id As Integer
        Public title As String
        Public txt As String
        Public imgPath As String




        Public Property pauthor() As Integer
            Get
                Return author
            End Get
            Set(ByVal value As Integer)
                author = value
            End Set
        End Property

        Public Property pid() As Integer
            Get
                Return id
            End Get
            Set(ByVal value As Integer)
                id = value
            End Set
        End Property

        Public Property ptitle() As String
            Get
                Return title
            End Get
            Set(ByVal value As String)
                title = value
            End Set
        End Property

        Public Property ptxt() As String
            Get
                Return txt
            End Get
            Set(ByVal value As String)
                txt = value
            End Set
        End Property
        Public Property pimgPath() As String
            Get
                Return imgPath
            End Get
            Set(ByVal value As String)
                imgPath = value
            End Set
        End Property
    End Class

End Class