Permudious
New Member
- Dec 28, 2014
- 25
- 1
Hi there,
I have written a code which seems like working but in c# I can't post a JSON request. When I do the same post in Postman, it is working fine.
What do I wrong?
I have written a code which seems like working but in c# I can't post a JSON request. When I do the same post in Postman, it is working fine.
What do I wrong?
C#:
string username = "demo";
string password = "password";
string firstname = "John";
string lastname = "Smith";
string url = "http://localhost/index.php";
//setup some variables end
string result = "";
String strPost = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname;
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
myWriter.Close();
}