tags:

views:

12

answers:

0

I have a simple OpenRasta webservice and a console client for the webservice.

Using GET method is quite easy - I defined GET in OpenRasta and when client uses this code it all works fine

 HttpWebRequest request = WebRequest.Create("http://localhost:56789/one/two/three") as HttpWebRequest;  

 // Get response  
 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
 {  
     // Get the response stream  
     StreamReader reader = new StreamReader(response.GetResponseStream());  

     // Console application output  
     Console.WriteLine(reader.ReadToEnd());  

However when I try to use POST like this

  Uri address = new Uri("http://localhost:56789/");

  HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
  request.Method = "POST";
  request.ContentType = "application/x-www-form-urlencoded";

  string one = "one";
  string two = "two";
  string three = "three";

  StringBuilder data = new StringBuilder();
  data.Append(HttpUtility.UrlEncode(one));
  data.Append("/" + HttpUtility.UrlEncode(two));
  data.Append("/" + HttpUtility.UrlEncode(three));

  byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
  request.ContentLength = byteData.Length;

  // Write data  
  using (Stream postStream = request.GetRequestStream())
  {
    postStream.Write(byteData, 0, byteData.Length);
  }

  // Get response  
  using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  {
    StreamReader reader = new StreamReader(response.GetResponseStream());
    Console.WriteLine(reader.ReadToEnd());
  }
  Console.ReadKey();
}

I get 500 Internal Server Error and I have no idea how to handle this in OpenRasta webservice. How do I defined POST method in Openrasta? Any suggestions?