• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Labview创建webservice和C#进行的http通信(post,get)

武飞扬头像
读书人张七七
帮助1

手里有个项目需要labview与C#使用http进行通讯,简单写了个demo,C#是个半吊子水平,记录一下过程方便以后查阅

1.labview webservice的创建和编写

有关使用labview创建webservice的方法与实例网上有很多,这里主要介绍Post,Get方法的创建和使用,如果是之前没有相关的通讯经验,推荐简单了解一下基本概念再进行程序编写

HTTP 教程 | 菜鸟教程HTTP 教程 HTTP 协议一般指 HTTP(超文本传输协议)。 超文本传输协议(英语:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式、协作式和超媒体信息系统的应用层协议,是因特网上应用最为广泛的一种网络传输协议,所有的 WWW 文件都必须遵守这个标准。 HTTP 是为 Web 浏览器与 Web 服务器之间的通信而设计的,但也可以用于其他目的。 HTTP 是一个基于 TCP/IP 通信协议..学新通https://www.runoob.com/http/http-tutorial.html这边创建两个方法,一个Post一个Get,实现一个简单的加减乘除计算器

学新通

 在web资源新建vi后,在右键菜单里选择这个vi是什么方法

学新通

Get方法只是简单的把当前Post的数据返回,这里使用json字符串进行传递,传递的结构如下

学新通

 学新通

 需要注意两点,首先生成exe之后,存在于webservice中方法vi里的全局变量好像失效了(需要的朋友们自己再验证一下,我这里只是简单的测试),所以使用了功能型全局变量,也就是类似labview中自带的值改变函数(2017以及之后才有),使用while循环里移位寄存器进行存储

这是labview自带的值改变函数

学新通

这是我自己写的功能型全局变量

 学新通

下面是Post方法,读取传入的ison字符解析后进行计算,然后返回

 学新通

 这样,post,get方法就创建好了,其中一些东西需要在webservice右键属性进行修改

学新通

这里同样可以修改vi的方法,以及url的构成方式

学新通

 这个界面如果选择ison,会把输出的response在用json转换一层,所以我还是用了简单的方法,自己生成json,直接输出text,下面的数据流选项如果不勾选,需要在方法vi的接线板上连接response,才能有response的输出

 至此,labview上web服务器的创建就已经完成,右键发布再右键开始即可进行其他客户端的代码调试

2.labview客户端编写

先贴界面

学新通

 后面板截图如下,简单的使用封装vi进行通讯,就不详细说明了

学新通

 学新通

 3.C#客户端的编写

界面与labview客户端基本一致

学新通

 MainWindow代码

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using System.Threading.Tasks;
  6.  
    using System.Windows;
  7.  
    using System.Windows.Controls;
  8.  
    using System.Windows.Data;
  9.  
    using Newtonsoft.Json;
  10.  
     
  11.  
    namespace HttpTest
  12.  
    {
  13.  
    /// <summary>
  14.  
    /// Interaction logic for MainWindow.xaml
  15.  
    /// </summary>
  16.  
    public partial class MainWindow : Window
  17.  
    {
  18.  
     
  19.  
    public MainWindow()
  20.  
    {
  21.  
    InitializeComponent();
  22.  
    }
  23.  
    private void windowload(object sender, RoutedEventArgs e)
  24.  
    {
  25.  
    string[] op = new string[] {"Add", "Subtract", "Multiply", "Divide" };
  26.  
    ipaddress.Text = "127.0.0.1";
  27.  
    port.Text = "8080";
  28.  
    timeout.Text = "2000";
  29.  
    A.Text = "";
  30.  
    B.Text = "";
  31.  
    operation.ItemsSource = op;
  32.  
    operation.Text = op[0];
  33.  
    rev.Text = "";
  34.  
    }
  35.  
    private HttpTest.http.httpser webser = new http.httpser();
  36.  
    class httpjsonstruct
  37.  
     
  38.  
    {
  39.  
     
  40.  
    public string? a { get; set; }
  41.  
     
  42.  
    public string? b { get; set; }
  43.  
     
  44.  
    public string? operation { get; set; }
  45.  
     
  46.  
    }
  47.  
    private void post_Click(object sender, RoutedEventArgs e)//Response=333.00
  48.  
    {
  49.  
    httpjsonstruct ob = new httpjsonstruct() { a = A.Text, b = B.Text, operation = operation.Text };
  50.  
    string jsonString = JsonConvert.SerializeObject(ob, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
  51.  
    int time = Convert.ToInt32(timeout.Text);
  52.  
    rev.Text = webser.HttpPost("http://" ipaddress.Text ":" port.Text "/BasicOperation/Update_Operand", jsonString, time);//http://127.0.0.1:8001/BasicOperation/Update_Operand
  53.  
    }
  54.  
     
  55.  
    private void get_Click(object sender, RoutedEventArgs e)//Response={"a":"12","b":"24","operation":"Add"}
  56.  
    {
  57.  
    int time = Convert.ToInt32(timeout.Text);
  58.  
    string getstring = webser.HttpGet("http://" ipaddress.Text ":" port.Text "/BasicOperation/Get_Content", time);
  59.  
    getstring = getstring.Substring(getstring.IndexOf('=') 1);
  60.  
    httpjsonstruct? getcontent = JsonConvert.DeserializeObject(getstring, typeof(httpjsonstruct)) as httpjsonstruct;
  61.  
    rev.Text = "a=" getcontent?.a "\r\n" "b=" getcontent?.b "\r\n" "operation=" getcontent?.operation "\r\n";
  62.  
    }
  63.  
    }
  64.  
    }
学新通

添加了一个自定义的类,用来写http的post和get方法

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using System.Threading.Tasks;
  6.  
    using System.Web;
  7.  
    using System.Net;
  8.  
    using System.IO;
  9.  
     
  10.  
    namespace HttpTest
  11.  
    {
  12.  
    public static class http
  13.  
    {
  14.  
    public static string receive = "";
  15.  
    public class httpser
  16.  
    {
  17.  
    public string HttpPost(string Url, string postDataStr, int timeout)
  18.  
    {
  19.  
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  20.  
    request.Timeout = timeout;
  21.  
    request.Method = "POST";
  22.  
    request.ContentType = "application/x-www-form-urlencoded";
  23.  
    Encoding encoding = Encoding.UTF8;
  24.  
    byte[] postData = encoding.GetBytes(postDataStr);
  25.  
    request.ContentLength = postData.Length;
  26.  
    Stream HttpRequestStream = request.GetRequestStream();
  27.  
    HttpRequestStream.Write(postData, 0, postData.Length);
  28.  
    HttpRequestStream.Close();
  29.  
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  30.  
    Stream HttpResponseStream = response.GetResponseStream();
  31.  
    StreamReader myStreamReader = new StreamReader(HttpResponseStream, encoding);
  32.  
    string retString = myStreamReader.ReadToEnd();
  33.  
    myStreamReader.Close();
  34.  
    HttpResponseStream.Close();
  35.  
     
  36.  
    return retString;
  37.  
    }
  38.  
    public string HttpGet(string Url, int timeout)
  39.  
    {
  40.  
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  41.  
    request.Timeout = timeout;
  42.  
    request.Method = "GET";
  43.  
    request.ContentType = "text/html;charset=UTF-8";
  44.  
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  45.  
    Stream HttpResponseStream = response.GetResponseStream();
  46.  
    StreamReader myStreamReader = new StreamReader(HttpResponseStream, Encoding.GetEncoding("utf-8"));
  47.  
    string retString = myStreamReader.ReadToEnd();
  48.  
    myStreamReader.Close();
  49.  
    HttpResponseStream.Close();
  50.  
    return retString;
  51.  
    }
  52.  
    }
  53.  
     
  54.  
    }
  55.  
    }
学新通

全部完成后,最终实现的效果如下学新通

源代码地址

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcabfi
系列文章
更多 icon
同类精品
更多 icon
继续加载