Ellen Hu

Ellen Hu

  • NA
  • 63
  • 0

WebRequest.GetRequestStream() -- error This stream does not support seek operations.

Sep 15 2010 5:46 PM
Hi All,

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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"? ><auth_request><user><email>" + email + "</email><password>" + password + "</password></user><api_key>" + api_key + "</api_key></ auth_request>";
               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

Answers (1)