.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侧边栏式窗体是如何实现的?下一节会讲到。

相关推荐
瓜皮弟子头很铁11 分钟前
.net 公共变量 线程安全
.net
江沉晚呤时3 小时前
C# 实现雪花算法(Snowflake Algorithm)详解与应用
c#·.net
CodeJourney.6 小时前
ChemBlender:科研绘图创新解决方案
数据库·人工智能·信息可视化·excel
olllo.top9 小时前
从 Excel 到 Data.olllo:数据分析师的提效之路
数据分析·excel
appleคิดถึง9 小时前
wps excel将表格输出pdf时所有列在一张纸上
pdf·excel·wps
Dxy123931021610 小时前
python如何设置excel单元格边框样式
开发语言·python·excel
chaodaibing11 小时前
Python解析Excel入库如何做到行的拆分
开发语言·python·excel
bicijinlian15 小时前
.Net HttpClient 使用代理功能
c#·.net·httpclient·.net httpclient·httpclient 代理
界面开发小八哥21 小时前
界面控件DevExpress WinForms v24.2 - 数据处理功能增强
.net·界面控件·devexpress·ui开发·winforms
kovlistudio1 天前
机器学习第八讲:向量/矩阵 → 数据表格的数学表达,如Excel表格转数字阵列
机器学习·矩阵·excel