栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Flask使用------python算法后端连接c#Winform前端

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flask使用------python算法后端连接c#Winform前端

Flask使用------python算法后端连接c#Winform前端

前段时间,一直在纠结怎么将Ubuntu系统的python算法和Windows系统的C#连接起来,后面在经过搜了很多资料后,发现了Flask这个神奇的东西。学了几天,做出了自己需要的东西,就没有继续深入学习下去了,毕竟现在没有很多时间去学习。等暑期可以深入学一下,还挺有意思的。

我在网上搜集了一些有关python和C#用Flask连接的例子,但是没有我想要的,索性自己边看边捣鼓也差不多捣鼓出来了。就把代码贴到这里了。

前提:我的python算法需要传出的是图片,其传入的有时是图片,有时是string(这里只放了前端代码,后端代码在之前的博客里面贴出来啦!)

前端代码:
 		/// 
        ///  通过网络地址和端口访问数据:image型--image型
        /// 
        /// 网络地址
        /// json参数
        /// 
        public Image RequestsPostImage(string Url, byte[] jsonParas)
        {
            string postContent = "";
            string strUrl = Url;
            Image img = null;

            //创建一个HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            //Post请求方式
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/json";

            //设置参数,并进行URL编码
            byte[] payLoad = jsonParas;
  
            //设置请求的ContentLength
            request.ContentLength = payLoad.Length;

            //发送请求
            Stream writer;
            try
            {
                //获取用于写入请求数据的Stream对象
                writer = request.GetRequestStream();
            }
            catch (Exception)
            {
                writer = null;
                MessageBox.Show("连接服务器失败");
                return null;
            }
            //将请求参数写入流
            writer.Write(payLoad, 0, payLoad.Length);
            //关闭请求流
            writer.Close();

            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
                postContent = "default: The response is null." + "Exception: " + ex.Message;
                MessageBox.Show(postContent);
            }
            if (response != null)
            {
                try
                {
                    Stream s = response.GetResponseStream();
                    img = Image.FromStream(s);
                }
                catch (Exception e)
                {
                    postContent = "default: The data stream is not readable." + "rn" + e.Message;
                    MessageBox.Show(postContent);
                }
            }
            //返回JSON数据
            return img;
        }

        /// 
        ///  通过网络地址和端口访问数据:string型--image型
        /// 
        /// 网络地址
        /// json参数
        /// 
        public Image RequestsPostString(string Url, string jsonParas)
        {
            string postContent = "";
            string strUrl = Url;
            Image img = null;

            //创建一个HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            //Post请求方式
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/json";

            //设置参数,并进行URL编码
            string paraUrlCoded = jsonParas;
            byte[] payLoad;
            //将json字符串转化为字节
            payLoad = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength
            request.ContentLength = payLoad.Length;

            //发送请求
            Stream writer;
            try
            {
                //获取用于写入请求数据的Stream对象
                writer = request.GetRequestStream();
            }
            catch (Exception)
            {
                writer = null;
                MessageBox.Show("连接服务器失败");
                return null;
            }
            //将请求参数写入流
            writer.Write(payLoad, 0, payLoad.Length);
            //关闭请求流
            writer.Close();

            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
                postContent = "default: The response is null." + "Exception: " + ex.Message;
                MessageBox.Show(postContent);
            }
            if (response != null)
            {
                try
                {
                    Stream s = response.GetResponseStream();
                    img = Image.FromStream(s);
                }
                catch (Exception e)
                {
                    postContent = "default: The data stream is not readable." + "rn" + e.Message;
                    MessageBox.Show(postContent);
                }
            }
            //返回JSON数据
            return img;
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号