I am trying to send data to the server and get the response. The only problem I am having is that I am unsure how the parameters work for passing in data. How do I send an HTTP POST with data AND get the response's content? I would be sending a username and a company name to the server and getting the information back.
public enum Verb
{
GET,
POST,
PUT,
DELETE
}
namespace API_Call_Tests
{
class Program
{
static void Main(string[] args)
{
var client = new Client();
client.EndPoint = @"http://stackoverflow.com/getToken.groovy";
client.Method = Verb.GET;
var response = client.Request();
var result = JsonConvert.DeserializeObject<Result>(response);
Console.WriteLine("This is seperate");
Console.WriteLine( result.token +result.message +result.success);
}
public class Result
{
public string token { get; set; }
public string message { get; set; }
public bool success { get; set; }
}
public class Client
{
public string EndPoint { get; set; }
public Verb Method { get; set; }
public string ContentType { get; set; }
public string PostData { get; set; }
public Client()
{
EndPoint = "";
Method = Verb.GET;
ContentType = "application/JSON";
PostData = "";
}
public Client(string endpoint, Verb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = "text/json";
PostData = postData;
}
public string Request()
{
return Request("");
}
public string Request(string parameters)
{
var request = (HttpWebRequest) WebRequest.Create(EndPoint);
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
using (var response = (HttpWebResponse) request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Faile: Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
}
}
}