Flask使用------python算法后端连接c#Winform前端
Flask使用------python算法后端连接c#Winform前端
前段时间,一直在纠结怎么将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;
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgahbih
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01