赞
踩
前段时间,一直在纠结怎么将Ubuntu系统的python算法和Windows系统的C#连接起来,后面在经过搜了很多资料后,发现了Flask这个神奇的东西。学了几天,做出了自己需要的东西,就没有继续深入学习下去了,毕竟现在没有很多时间去学习。等暑期可以深入学一下,还挺有意思的。
我在网上搜集了一些有关python和C#用Flask连接的例子,但是没有我想要的,索性自己边看边捣鼓也差不多捣鼓出来了。就把代码贴到这里了。
前提:我的python算法需要传出的是图片,其传入的有时是图片,有时是string(这里只放了前端代码,后端代码在之前的博客里面贴出来啦!)
前端代码: /// <summary> /// 通过网络地址和端口访问数据:image型--image型 /// </summary> /// <param name="Url">网络地址</param> /// <param name="jsonParas">json参数</param> /// <returns></returns> 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." + "\r\n" + e.Message; MessageBox.Show(postContent); } } //返回JSON数据 return img; } /// <summary> /// 通过网络地址和端口访问数据:string型--image型 /// </summary> /// <param name="Url">网络地址</param> /// <param name="jsonParas">json参数</param> /// <returns></returns> 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." + "\r\n" + e.Message; MessageBox.Show(postContent); } } //返回JSON数据 return img; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。