vb.net rest 接口 demo

下面是帮一位 使用vb.net的朋友做的一个最简单 rest接口示例 ,详细源码见文章底部

最终效果如下:

默认查询 返回json

http://localhost:54370/Home/Index

clip_image002

clip_image004

 

 

默认创建表

http://localhost:54370/home/createtable

clip_image002[8]

clip_image004[10]

 

单记录插表

clip_image002[10]

测试调用代码

clip_image004[12]

clip_image006

多记录上传插表

clip_image008

测试调用代码

clip_image010

clip_image012

 

 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

  

 

posted @ 2023-03-16 18:55  网络来者  阅读(82)  评论(0编辑  收藏  举报