.net实战(VB):连接网页数据

先上效果图

在上程序

vbnet 复制代码
Public Sub SendPostRequest(aa As String)
    'MessageBox.Show(sheet.Name)
    Dim token As String
    Dim xmlhttp As Object
    Dim xmlhttp1 As Object
    Dim url As String
    Dim jsonParams As String
    Dim response As String
    xmlhttp = CreateObject("MSXML2.XMLHTTP")
    url = "http://newstocktest.xxx.com.cn:8088/inventory/login"
    jsonParams = "{""accountId"": ""hai.miao"", ""accountPwd"": ""Mh111"", ""lang"": ""zh_CN""}"
    xmlhttp.Open("POST", url, False)
    xmlhttp.setRequestHeader("Content-Type", "application/json")
    xmlhttp.send(jsonParams)
    response = xmlhttp.responseText
    ' 处理响应数据
    'MsgBox(response)
    Debug.WriteLine(response)
    For i = 0 To UBound(Split(response, ","))
        If InStr(Split(response, ",")(i), "token") > 0 Then
            token = Split(Split(response, ",")(i), ":")(1).Replace("""", "")
            Exit For
        End If
    Next
    'Thread.Sleep(5000)
    xmlhttp1 = CreateObject("MSXML2.XMLHTTP")
    url = "http://newstocktest.xxx.com.cn:8088/inventory/invertory/list"
    Debug.WriteLine(token)
    Dim aa1 = aa
    jsonParams = "{""name2"":""" & aa1 & """}"
    Debug.WriteLine(jsonParams)
    xmlhttp1.Open("POST", url, False)
    xmlhttp1.setRequestHeader("Content-Type", "application/json")
    xmlhttp1.setRequestHeader("tokenkey", token)
    xmlhttp1.send(jsonParams)
    response = xmlhttp1.responseText
    Debug.WriteLine(response)
    Dim o As JObject = JObject.Parse(response)
    Dim results As List(Of JToken) = o.Children().ToList
    userAbout.TextBox1.Text = aa1
    userAbout.DataGridView1.DataSource = Nothing
    userAbout.DataGridView1.Rows.Clear()
    For Each item As JProperty In results
        item.CreateReader()
        Select Case item.Name
            Case "data"
                'Debug.WriteLine(item.Value)
                Dim o1 As JObject = JObject.Parse(item.Value.ToString)
                Dim results1 As List(Of JToken) = o1.Children().ToList
                For Each item1 As JProperty In results1
                    item1.CreateReader()
                    Select Case item1.Name
                        Case "content"
                            Dim i = 1
                            For Each subitem As JObject In item1.Values
                                Debug.WriteLine(subitem("materCode"))
                                Debug.WriteLine(subitem("materCndesc"))
                                Dim rowIndex As Integer = userAbout.DataGridView1.Rows.Add()
                                userAbout.DataGridView1.Rows(rowIndex).Cells(0).Value = i
                                userAbout.DataGridView1.Rows(rowIndex).Cells(1).Value = subitem("materCode")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(2).Value = subitem("materCndesc")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(3).Value = subitem("materAssist")
                                'userAbout.DataGridView1.Rows(rowIndex).Cells(4).Value = subitem("materZl")
                                'userAbout.DataGridView1.Rows(rowIndex).Cells(5).Value = subitem("stockNum")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(6).Value = subitem("stockNum")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(7).Value = subitem("materZl")

                                i = i + 1
                            Next
                        Case "totalElements"
                    End Select
                Next
        End Select
    Next
    'MessageBox.Show("hello")
    Debug.WriteLine("hello")
    'MessageBox.Show(sheet.Name)
    Debug.WriteLine(sheet.Name)
End Sub

解析

1.aa是需要查询的字符串

2.定义一个网页连接,网址,传入的参数;然后打开网址,发送请求,传参。response是获取到返回的文本。如何获取可以传参的网页地址,要传入哪些参数,因网页不同而不同,具体方法请另外私信博主。此处是实现登录并获取返回的数据(主要是为了获取令牌,后续的数据获取都要在传参的同时传入令牌)

vbnet 复制代码
    xmlhttp = CreateObject("MSXML2.XMLHTTP")
    url = "http://newstocktest.xxx.com.cn:8088/inventory/login"
    jsonParams = "{""accountId"": ""hai.miao"", ""accountPwd"": ""Mh111"", ""lang"": ""zh_CN""}"
    xmlhttp.Open("POST", url, False)
    xmlhttp.setRequestHeader("Content-Type", "application/json")
    xmlhttp.send(jsonParams)
    response = xmlhttp.responseText

3.对返回的数据进行处理,获取token(令牌)

vbnet 复制代码
    For i = 0 To UBound(Split(response, ","))
        If InStr(Split(response, ",")(i), "token") > 0 Then
            token = Split(Split(response, ",")(i), ":")(1).Replace("""", "")
            Exit For
        End If
    Next

Debug.WriteLine(token)输出的数据如下

43531674-36de-4d4a-a940-db2bd1d2c711

4.登录进去以后,在相应的控件中继续传参,我这个实例中控件的id是name2,传入aa1字符串。这里要注意,发送请求时要传入token(令牌),否则时不会返回数据的。

vbnet 复制代码
    xmlhttp1 = CreateObject("MSXML2.XMLHTTP")
    url = "http://newstocktest.xxx.com.cn:8088/inventory/invertory/list"
    Debug.WriteLine(token)
    Dim aa1 = aa
    jsonParams = "{""name2"":""" & aa1 & """}"
    Debug.WriteLine(jsonParams)
    xmlhttp1.Open("POST", url, False)
    xmlhttp1.setRequestHeader("Content-Type", "application/json")
    xmlhttp1.setRequestHeader("tokenkey", token)
    xmlhttp1.send(jsonParams)
    response = xmlhttp1.responseText

补充一句,传参的网址和我们手动登录的网址是不一样的,要获取常规网站的传参网址,请私信博主。

5.Debug.WriteLine(response)输出的数据如下,将这一串数据赋值给JObject变量,获取child(子项),这里面我们主要获取data子项

vbnet 复制代码
{"status":"SUCCESS","message":"加载完成","data":{"content":[{"id":59679,"materCode":"11726877","materCndesc":"截止阀,1GLN3R V3,S43/38,$$$$","materEndesc":"GLOBE VALVE,1GLN3R V3,S43/38,$$$$","materAssist":"[-GTA-I-IF-MOC-PT,-MOU-B8A/8A.A]","materVspec":"GLN-针形截止阀","materVspecen":"NEEDLE GLOBE VALVE","materCaliber":"1","materPress":"CLASS 300","materConn":"R-突面法兰","materConnen":"R-RAISED FACE END","materShell":"ASTM A182 F316L","materTexture":"S43","materInter":"F316L/HF","optType":"HW","materOpt":null,"materOpten":null,"crtdate":null,"crtuserid":null,"modifydate":null,"muserid":null,"cmaterId":null,"stockNum":1,"custId":null,"custName":null,"custCode":null,"cmaterVspec":null,"cmaterCode":null,"cmaterDesc":null,"cmaterSpecs":null,"cmaterUnit":null,"materLgort":"CHINA","materVbeln":"86592","materPosnr":"131","lineMemo":"/体盖:氧阀订单,O标识,硬度测试:10%数量,晶间腐蚀:ASTM A262 E法,林德新加坡项目,3A标识/阀瓣:硬度测试:10%数量,晶间腐蚀:ASTM A262 E法/焊缝:焊后热处理,体圈AW-PT,硬度测试:10%数量/阀杆:硬度测试:10%数量,晶间腐蚀:ASTM A262 E法/清洗/测试要求:氧气阀清洗/油漆系统:不油漆/挂牌显示内容:GLOBE VALVE/R87853/0012AP0940/3BR112/ASME//阀门整机其他要求:86592-130,131所有阀门务必保证 相同炉号;不与介质接触的螺纹与键处,以及其他必要部位,如果要使用润滑,必须使用氧用LCO36  ","auart":null,"zkclx":null,"materZl":"483","frozenNum":0.0},{"id":59681,"materCode":"11726877","materCndesc":"截止阀,1GLN3R V3,S43/38,$$$$","materEndesc":"GLOBE VALVE,1GLN3R V3,S43/38,$$$$","materAssist":"[-GTA-I-IF-MOC-PT,-MOU-B8A/8A.A]","materVspec":"GLN-针形截止阀","materVspecen":"NEEDLE GLOBE VALVE","materCaliber":"1","materPress":"CLASS 300","materConn":"R-突面法兰","materConnen":"R-RAISED FACE END","materShell":"ASTM A182 F316L","materTexture":"S43","materInter":"F316L/HF","optType":"HW","materOpt":null,"materOpten":null,"crtdate":null,"crtuserid":null,"modifydate":null,"muserid":null,"cmaterId":null,"stockNum":1,"custId":null,"custName":null,"custCode":null,"cmaterVspec":null,"cmaterCode":null,"cmaterDesc":null,"cmaterSpecs":null,"cmaterUnit":null,"materLgort":"CHINA","materVbeln":"86592","materPosnr":"31","lineMemo":"/体盖:氧阀订单,O标识/清洗/测试要求:氧气阀清洗/油漆系统:不锈钢不油漆/阀门整机其他要求:该订单所有阀门务必保证相同炉号;不与介质接触的螺纹与键处,以及其他必要部位,如果要使用润滑,必须使用氧用LCO36   ","auart":null,"zkclx":null,"materZl":"812","frozenNum":0.0},{"id":59680,"materCode":"11726877","materCndesc":"截止阀,1GLN3R V3,S43/38,$$$$","materEndesc":"GLOBE VALVE,1GLN3R V3,S43/38,$$$$","materAssist":"[-GTA-I-IF-MOC-PT,-MOU-B8A/8A.A]","materVspec":"GLN-针形截止阀","materVspecen":"NEEDLE GLOBE VALVE","materCaliber":"1","materPress":"CLASS 300","materConn":"R-突面法兰","materConnen":"R-RAISED FACE END","materShell":"ASTM A182 F316L","materTexture":"S43","materInter":"F316L/HF","optType":"HW","materOpt":null,"materOpten":null,"crtdate":null,"crtuserid":null,"modifydate":null,"muserid":null,"cmaterId":null,"stockNum":1,"custId":null,"custName":null,"custCode":null,"cmaterVspec":null,"cmaterCode":null,"cmaterDesc":null,"cmaterSpecs":null,"cmaterUnit":null,"materLgort":"CHINA","materVbeln":"86592","materPosnr":"61","lineMemo":"/体盖:氧阀订单,O标识,硬度测试:10%数量/阀瓣:硬度测试:10%数量/焊缝:焊后热处理,体圈AW-PT,硬度测试:10%数量/阀杆:硬度测试:10%数量/清洗/测试要求:氧气阀清洗/油漆系统:不锈钢不油漆/挂牌显示内容:GLOBE VALVE/R87853/0012AP0590/3BR112/ASME//阀门整机其他要求:86592-60,61所有阀门务必保证相同炉号;不与介质接触的螺纹与键处,以及其他必要部位,如果要使用润滑,必须使用氧用LCO36   ","auart":null,"zkclx":null,"materZl":"755","frozenNum":0.0}],"totalElements":3},"code":200}

6.data子项下还有子项,我们还要获取content子项,代码体现在如下部分。

vbnet 复制代码
    Dim o As JObject = JObject.Parse(response)
    Dim results As List(Of JToken) = o.Children().ToList
    userAbout.TextBox1.Text = aa1
    userAbout.DataGridView1.DataSource = Nothing
    userAbout.DataGridView1.Rows.Clear()
    For Each item As JProperty In results
        item.CreateReader()
        Select Case item.Name
            Case "data"
                'Debug.WriteLine(item.Value)
                Dim o1 As JObject = JObject.Parse(item.Value.ToString)
                Dim results1 As List(Of JToken) = o1.Children().ToList
                For Each item1 As JProperty In results1
                    item1.CreateReader()
                    Select Case item1.Name
                        Case "content"

7.获取到content后,根据每个数据的名称填入datagridview控件

vbnet 复制代码
                        Case "content"
                            Dim i = 1
                            For Each subitem As JObject In item1.Values
                                Debug.WriteLine(subitem("materCode"))
                                Debug.WriteLine(subitem("materCndesc"))
                                Dim rowIndex As Integer = userAbout.DataGridView1.Rows.Add()
                                userAbout.DataGridView1.Rows(rowIndex).Cells(0).Value = i
                                userAbout.DataGridView1.Rows(rowIndex).Cells(1).Value = subitem("materCode")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(2).Value = subitem("materCndesc")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(3).Value = subitem("materAssist")
                                'userAbout.DataGridView1.Rows(rowIndex).Cells(4).Value = subitem("materZl")
                                'userAbout.DataGridView1.Rows(rowIndex).Cells(5).Value = subitem("stockNum")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(6).Value = subitem("stockNum")
                                userAbout.DataGridView1.Rows(rowIndex).Cells(7).Value = subitem("materZl")

                                i = i + 1
                            Next

此项目中Excel侧边栏式窗体是如何实现的?下一节会讲到。

相关推荐
九鼎科技-Leo7 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
开心点幸运点10 小时前
Excel——宏教程(1)
excel
dot.Net安全矩阵12 小时前
.NET 通过模块和驱动收集本地EDR的工具
windows·安全·web安全·.net·交互
zls36536512 小时前
.NET开源实时应用监控系统:WatchDog
.net
djk888812 小时前
.net将List<实体1>的数据转到List<实体2>
数据结构·list·.net
Crazy Struggle17 小时前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
boy快快长大19 小时前
将大模型生成数据存入Excel,并用增量的方式存入Excel
java·数据库·excel
时光追逐者19 小时前
.NET 9 中 LINQ 新增功能实操
开发语言·开源·c#·.net·.netcore·linq·微软技术
Leuanghing19 小时前
使用Python生成F分布表并导出为Excel文件
开发语言·python·excel·f分布
爱编程的小生19 小时前
Easyexcel(4-模板文件)
java·excel