C# 如何在C中通过伪REST服务触发GET请求#

C# 如何在C中通过伪REST服务触发GET请求#,c#,.net,xml,http,C#,.net,Xml,Http,我需要与遗留php应用程序通信。API只是一个php脚本,它接受get请求并以XML形式返回响应 我想用C#写这封信 触发GET请求(具有许多参数)然后解析结果的最佳方法是什么? 理想情况下,我希望找到一些简单的东西,如下面的python代码: params = urllib.urlencode({ 'action': 'save', 'note': note, 'user': user, 'passwd': passwd, }) content = urll

我需要与遗留php应用程序通信。API只是一个php脚本,它接受get请求并以XML形式返回响应

我想用C#写这封信

触发GET请求(具有许多参数)然后解析结果的最佳方法是什么?

理想情况下,我希望找到一些简单的东西,如下面的python代码:

params = urllib.urlencode({
    'action': 'save',
    'note': note,
    'user': user,
    'passwd': passwd,
 })

content = urllib.urlopen('%s?%s' % (theService,params)).read()
data = ElementTree.fromstring(content)
...
更新: 我正在考虑使用XElement.Load,但我看不到一种轻松构建GET查询的方法。

一个简单的函数在功能上类似于
python
urllib

C#
示例(从上面的参考中稍微编辑)显示了如何“触发GET请求”:


要解析结果,请使用System.XML类或更好的类。一个简单的可能性是该方法-您可以直接使用由
OpenRead()
返回的
WebClient
流。

中有一些很好的实用程序类,用于实现调用在任何平台上实现的服务的.NET REST客户端

这描述了如何使用客户端部件

示例代码片段:

HttpClient c = new HttpClient("http://twitter.com/statuses");
c.TransportSettings.Credentials = 
    new NetworkCredentials(username, password);
// make a GET request on the resource.
HttpResponseMessage resp = c.Get("public_timeline.xml");
// There are also Methods on HttpClient for put, delete, head, etc
resp.EnsureResponseIsSuccessful(); // throw if not success
// read resp.Content as XElement
resp.Content.ReadAsXElement(); 

你不知道有更好的.net url生成器吗?您正在生成的url无效。尽管如此,您仍然使用“?”而不是“&”,您还没有逃逸参数。很抱歉,示例改为使用HttpUtility.UrlEncode(字符串,编码)。我认为,WCF REST初学者工具包中包含了一个url生成器。
HttpClient c = new HttpClient("http://twitter.com/statuses");
c.TransportSettings.Credentials = 
    new NetworkCredentials(username, password);
// make a GET request on the resource.
HttpResponseMessage resp = c.Get("public_timeline.xml");
// There are also Methods on HttpClient for put, delete, head, etc
resp.EnsureResponseIsSuccessful(); // throw if not success
// read resp.Content as XElement
resp.Content.ReadAsXElement();