我正在进行的当前项目需要从机器人到我的网站的双向通信。
假设示例URL是www.example.com/foobar.php或其他什么,你能解释一下如何从那里POST和获取数据吗?
非常感谢。
第页。S.-使用网络客户端对吗?
我建议使用RestSharp。它比使用WebClient容易得多,并为您提供了更多的选择:
var client = new RestClient("http://www.example.com/");
//to POST data:
var postRequest = new RestRequest("foo.php", Method.POST);
postRequest.AddParameter("name", "value");
var postResponse = client.Execute(postRequest);
//postResponse.Content will contain the raw response from the server
//To GET data
var getRequest = new RestRequest("foo.php", Method.GET);
getRequest.AddParameter("name", "value");
var getResponse = client.Execute(getRequest);
是的,您可以使用WebClient:
using (WebClient client = new WebClient())
{
NameValueCollection nvc = new NameValueCollection()
{
{ "foo", "bar"}
};
byte[] responseBytes = client.UploadValues("http://www.example.com/foobar.php", nvc);
string response = System.Text.Encoding.ASCII.GetString(responseBytes);
}
您可以使用WebClient
查找方法UploadString和DownloadString