Here is my code, I need make a call to a web server, key&email&password are passed to that server; I expect to have a "token" at WebResponse message.
WebRequest req = null;
WebResponse rsp = null;
try {
string email = "[email protected]";
string password = "8005case";
string api_key = "aada3015-2f84-4eab-bf00-4d5315fe2b05";
string xml = "
string uri = "https://api-test.globalgiving.org/api/userservice/tokens";
req = WebRequest.Create(uri);
req.Method = "POST"; // Post method
req.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(xml);
req.ContentLength = bytes.Length;
///error comes
Stream outputStream = req.GetRequestStream();
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Close();
rsp = req.GetResponse();
Stream MyStream = rsp.GetResponseStream();
StreamReader myStreamReader = new StreamReader(MyStream);
myStreamReader.ReadToEnd().Trim();
}
catch (WebException) {
}
catch (Exception) {
}
finally {
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}
I saw error comes right after req.GetRequestStream();, as the result the rsp won't get value. What should I modify?
Thank you in advance,
ellen
Mike GoldPosted Sep 16, 2010, 12:03 AM
Not sure this will help, but check out my article on the proper way to do a POST using a HttpWebRequest. Although it was written for silverlight talking to a REST API, you can apply it to any http web request.
http://www.c-sharpcorner.com/UploadFile/mgold/2295/Default.aspx