先上代码,出于保密程序中url无法使用
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports Microsoft.Office.Interop
Imports System.Net
Imports System.Text
Imports System.Diagnostics
Public Class ThisAddIn
Private Sub ThisAddIn_Startup() Handles Me.Startup
' 获取当前活动的工作表
Dim sheet As Excel.Worksheet = Globals.ThisAddIn.Application.ActiveSheet
' 为SelectionChange事件添加事件处理器
AddHandler sheet.SelectionChange, AddressOf Sheet_SelectionChange
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
Dim sheet As Excel.Worksheet = Globals.ThisAddIn.Application.ActiveSheet
RemoveHandler sheet.SelectionChange, AddressOf Sheet_SelectionChange
End Sub
Private Sub Sheet_SelectionChange(ByVal Target As Excel.Range)
' 这里处理选定单元格的逻辑
'MessageBox.Show("选定的单元格地址: " & Target.Address)
Call SendPostRequest()
End Sub
Sub SendPostRequest()
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 = "xxx"
jsonParams = "{""accountId"": ""hai.miao"", ""accountPwd"": ""Mh111116"", ""lang"": ""zh_CN""}"
xmlhttp.Open("POST", url, False)
xmlhttp.setRequestHeader("Content-Type", "application/json")
xmlhttp.send(jsonParams)
response = xmlhttp.responseText
' 处理响应数据
'MsgBox(response)
'Debug.Write(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
token = "909ac718-d89a-4a3a-94e0-9c0622cdf686"
xmlhttp1 = CreateObject("MSXML2.XMLHTTP")
url = "xxx"
Debug.Write(token)
jsonParams = "{""name2"": ""球阀,10x8B3R-G""}"
xmlhttp1.Open("POST", url, False)
xmlhttp1.setRequestHeader("Content-Type", "application/json")
xmlhttp1.setRequestHeader("tokenkey", token)
xmlhttp1.send(jsonParams)
response = xmlhttp1.responseText
Debug.Write(response)
End Sub
End Class