创建C#的wpf项目(CS架构),不知道CS架构中能不能用webapi,请高人解答,谢谢!

创建C#的wpf项目(CS架构),不知道CS架构中能不能用webapi,请高人解答,谢谢!,第1张

首先 ,你要明白浏览器也是客户端,只不过他提供的是一套通用的解释方式,具体执行程序放在了服务器上,服务器应用跟本地应用,本质上无差异,基本都是Sever+客户端处理,WPF是可以调用web api的 sever收到http请求可不管你是谁 是什么样的客户端发来的 只要是正确的http请求 就能响应,

使用HttpWebRequest类:

```C#

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/api/data")

request.Method = "GET"

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

{

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

string json = reader.ReadToEnd()

// Do something with the json

}

}

```

2. 使用WebClient类:

```C#

using (WebClient wc = new WebClient())

{

string json = wc.DownloadString("http://example.com/api/data")

// Do something with the json

}

```


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11266788.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存