I want to send a post request to an API with some parameters they ask for... I ended up just creating a string, it's ugly but I do not know a way of making it work differently. I then found lots of variations on this WebRequest
class, but unfortunately I cannot get it to work.
Main problem is probably because I am not really understanding how this is all fitting together but basically, the examples I have been following use WebRequest
method GetResponse
... even on MSDN it has this, so I am wondering why when I try to call it in my code, I am not getting that choice? Same goes for GetRequestStream
.
How to add parameters into a WebRequest?
*****DBContext()
{
data = "grant_type=" + GRANTTYPE + "&username=" + username + "&password=" + password + "&client_id=" + CLIENTID + "&redirect_uri=" + REDIRECTURI + "&client_secret=" + CLIENTSECRET;
}
public bool Authenticate()
{
byte[] dataStream = Encoding.UTF8.GetBytes(data);
WebRequest webRequest = WebRequest.Create(urlPath);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
// Send the data.
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
WebResponse webResponse = webRequest.GetResponse();
return true;
}
I also have the question of when I finally do get this stuff to work, what should I be putting in the callback uri. if it's a phone, is it running off of localhost?