vb.net rest 接口 demo
下面是帮一位 使用vb.net的朋友做的一个最简单 rest接口示例 ,详细源码见文章底部
最终效果如下:
默认查询 返回json
http://localhost:54370/Home/Index
默认创建表
http://localhost:54370/home/createtable
单记录插表
测试调用代码
多记录上传插表
测试调用代码
vs2019 新建mvc工程
服务端代码
Imports FreeSql
Imports Newtonsoft.Json
Public Class HomeController
Inherits System.Web.Mvc.Controller
Private Shared ykt As IFreeSql
Shared Sub New()
ykt = (New FreeSqlBuilder().UseAutoSyncStructure(True)).UseConnectionString(DataType.Sqlite, "Data Source=freedb.db", Nothing).Build()
End Sub
Public Function createtable() As String
Return ykt.[Select](Of tech_info)().Any().ToString()
End Function
Public Function Index() As ActionResult
Dim dict As IDictionary(Of String, Object) = New Dictionary(Of String, Object)() From
{
{"StateCode", 200}
}
Dim now As DateTime = DateTime.Now
dict.Add("Message", String.Concat("接口访问成功,服务器时间", now.ToString()))
Return MyBase.Json(dict, JsonRequestBehavior.AllowGet)
End Function
Public Function testUplaod(ByVal xx As List(Of ent_test)) As System.Web.Mvc.ActionResult
Dim actionResult As System.Web.Mvc.ActionResult
Dim retcode As Integer = -1
Dim t2 As Integer = 0
Dim key As String = DateTime.Now.ToString("yyyyMMddffff")
tools.log(String.Concat(key, "接口被调用----------测试上传方法testuplaod------------------------------"))
Dim sendstring As String = JsonConvert.SerializeObject(xx)
'tools.log(String.Concat(key, "传入数据为:", sendstring))
Try
t2 = ykt.Insert(Of ent_test)(xx).ExecuteAffrows()
tools.log(String.Concat(key, "数据插入行数-----", t2.ToString()))
Return MyBase.Json(New With {Key .StateCode = 200, Key .Message = String.Concat("测试上传成功!行数:", t2.ToString(), "数据为:", sendstring)}, JsonRequestBehavior.AllowGet)
Catch exception As System.Exception
Dim ex As System.Exception = exception
actionResult = MyBase.Json(New With {Key .StateCode = retcode, Key .Message = String.Concat(sendstring, ex.Message)}, JsonRequestBehavior.AllowGet)
End Try
Return actionResult
End Function
Public Function upload_tech_info(ByVal xx As tech_info) As System.Web.Mvc.ActionResult
Dim actionResult As System.Web.Mvc.ActionResult
Dim retcode As Integer = -1
Dim t2 As Integer = 0
Dim key As String = DateTime.Now.ToString("yyyyMMddffff")
Dim sendstring As String = JsonConvert.SerializeObject(xx)
tools.log(String.Concat(key, "接口被调用----------upload_tech_info-------", sendstring, "-----------------------"))
Try
t2 = ykt.Insert(Of tech_info)(xx).ExecuteAffrows()
Return MyBase.Json(New With {Key .StateCode = 200, Key .Message = String.Concat("上传工艺信息成功!行数:", t2.ToString(), "数据为:", sendstring)}, JsonRequestBehavior.AllowGet)
Catch exception As System.Exception
Dim ex As System.Exception = exception
actionResult = MyBase.Json(New With {Key .StateCode = retcode, Key .Message = String.Concat(sendstring, ex.Message)}, JsonRequestBehavior.AllowGet)
End Try
Return actionResult
End Function
End Class
测试客户端代码
Imports System.IO
Imports System.Net
Imports Newtonsoft.Json
Imports System.Text
Module Module1
Sub Main()
'印刷rest接口测试()
工单状态上传()
End Sub
'多记录上传
Private Sub 印刷rest接口测试()
Dim postdata As List(Of ent_test) = New List(Of ent_test)() From
{
New ent_test() With
{
.usercode = "001",
.username = "张三"
},
New ent_test() With
{
.usercode = "002",
.username = "李四"
},
New ent_test() With
{
.usercode = "003",
.username = "王五"
}
}
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://localhost:54370/home/testUplaod"), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/json"
Dim sendstring As String = JsonConvert.SerializeObject(postdata)
Dim data As Byte() = Encoding.UTF8.GetBytes(sendstring)
req.ContentLength = CLng(CInt(data.Length))
Using reqstream As Stream = req.GetRequestStream()
reqstream.Write(data, 0, CInt(data.Length))
reqstream.Close()
End Using
Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Dim text As String = String.Empty
Using responseStm As Stream = response.GetResponseStream()
text = (New StreamReader(responseStm, Encoding.UTF8)).ReadToEnd()
End Using
Console.WriteLine(text)
Console.ReadKey()
End Sub
'单记录上传
Private Sub 工单状态上传()
Dim postdata As tech_info = New tech_info() With
{
.id = 3,
.b = 4,
.c = 5,
.d = 6
}
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://localhost:54370/home/upload_tech_info"), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/json"
Dim sendstring As String = JsonConvert.SerializeObject(postdata)
Dim data As Byte() = Encoding.UTF8.GetBytes(sendstring)
req.ContentLength = CLng(CInt(data.Length))
Using reqstream As Stream = req.GetRequestStream()
reqstream.Write(data, 0, CInt(data.Length))
reqstream.Close()
End Using
Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Dim text As String = String.Empty
Using responseStm As Stream = response.GetResponseStream()
text = (New StreamReader(responseStm, Encoding.UTF8)).ReadToEnd()
End Using
Console.WriteLine(text)
Console.ReadKey()
End Sub
End Module



![clip_image002[8] clip_image002[8]](https://img2023.cnblogs.com/blog/133750/202303/133750-20230316190054805-1477992958.jpg)
![clip_image004[10] clip_image004[10]](https://img2023.cnblogs.com/blog/133750/202303/133750-20230316190055672-474778117.jpg)
![clip_image002[10] clip_image002[10]](https://img2023.cnblogs.com/blog/133750/202303/133750-20230316190056524-1495205705.jpg)
![clip_image004[12] clip_image004[12]](https://img2023.cnblogs.com/blog/133750/202303/133750-20230316190057447-858835571.jpg)




浙公网安备 33010602011771号