网络编程
位置:首页>> 网络编程>> Python编程>> C#调用Python的URL接口的示例

C#调用Python的URL接口的示例

作者:有翅膀的大象  发布时间:2022-08-22 21:49:27 

标签:c#,调用,python,url,接口

VS2013的简单WInForm控件,通过WebRequest,WebResponse来访问,接收:

C#调用Python的URL接口的示例


private void btn_interface_Click(object sender, EventArgs e)
   {
     string url = "http://127.0.0.1:5000";
     WebRequest wRequest = WebRequest.Create(url);
     wRequest.Method = "GET";
     wRequest.ContentType = "text/html;charset=UTF-8";
     WebResponse wResponse = wRequest.GetResponse();
     Stream stream = wResponse.GetResponseStream();
     StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
     string str = reader.ReadToEnd();  //url返回的值
     reader.Close();
     wResponse.Close();
   }

Python 简易接口:http://127.0.0.1:5000


from flask import Flask

#创建flask对象
app = Flask(__name__)

#创建路由'/'
@app.route('/')
def home():
 return "Hello,World!"
#当用户请求'/'资源时,回传"Hello,World!"

#启动flask,并设定端口为5000
app.run(port = 5000)

C#调用Python的URL接口的示例

基于这种访问方式,就可以用C#调用机器学习等人工智能及其它python业务接口了...

来源:https://www.cnblogs.com/shiningleo007/p/13534511.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com