vb 调用webservice

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值