简体   繁体   中英

How to handle C# .NET POST and GET commands

The current project I am working on requires a 2 way communication from the bot to my website.

Supposing the example URL is www.example.com/foobar.php or something, can you explain me how to POST and GET data from there?

Thanks a lot.

PS - Using webclient right?

I'd suggest using RestSharp . It's a lot easier than using WebClient, and gives you a lot more options:

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);

Yes, you can use 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);
} 

You can use WebClient

Look up method UploadString and DownloadString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM