C#調用Python的URL接口的示例
更新時間:2020年10月03日 10:52:08 作者:有翅膀的大象
這篇文章主要介紹了C#調用Python的URL接口的示例,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下
VS2013的簡單WInForm控件,通過WebRequest,WebResponse來訪問,接收:

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
#創(chuàng)建flask對象
app = Flask(__name__)
#創(chuàng)建路由'/'
@app.route('/')
def home():
return "Hello,World!"
#當用戶請求'/'資源時,回傳"Hello,World!"
#啟動flask,并設定端口為5000
app.run(port = 5000)

基于這種訪問方式,就可以用C#調用機器學習等人工智能及其它python業(yè)務接口了...
以上就是C#調用Python的URL接口的示例的詳細內容,更多關于C#調用Python的URL接口的資料請關注腳本之家其它相關文章!

