0

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.

P.S. - Using webclient right?

  • Possible duplicate http://stackoverflow.com/questions/4015324/http-request-with-post – mnme Apr 17 '14 at 14:03

3 Answers3

2

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);
Jedediah
  • 1,916
  • 16
  • 32
0

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);
} 
DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • So the `nvc` contains the params to look for in the URL?? and how can I parse response? Sorry. new to this stuff – user2794212 Apr 17 '14 at 14:05
  • The `NameValueCollection` are the parameters you want to post. My example shows how to get the response. – DGibbs Apr 17 '14 at 14:06
  • so the response string, whats the format of it containing the data? given the foo=1 and bar=2.. will the string be simply '12' ? – user2794212 Apr 17 '14 at 14:09
0

You can use WebClient

Look up method UploadString and DownloadString

Steve
  • 11,696
  • 7
  • 43
  • 81