使用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
}
```
有解决方案:建立CS程序与WebApi服务器的点对点连接(通过Tcp/ip),然后WebApi发起请求,CS程序返回请求结果。详细一点:CS端开启tcp.ip的监听端口,webapi服务器连接这个端口,需要掌握网络编程技能。百度搜索C# 网络编程相关的源码。
若CS端没有开启外网IP,还需要穿透NAT(类似QQ聊天,网络编程高级篇),这里有文章参考一下: C#实现UDP穿透NAT(UDP打洞)完整版(原)
www.csframework.com/archive/2/arc-2-20110713-1693.htm
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)