如何使用OpenRasta处理POST方法?

时间:2010-10-22 12:44:28

标签: post openrasta

我有一个简单的OpenRasta Web服务和Web服务的控制台客户端。

使用GET方法非常简单 - 我在OpenRasta中定义了GET,当客户端使用此代码时,它一切正常

 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());  

然而,当我尝试像这样使用POST时

  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();
}

我收到500内部服务器错误,我不知道如何在OpenRasta webservice中处理这个问题。如何在Openrasta中定义POST方法?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您提供的代码会发送“一/二/三”并将其放入您的请求内容中,媒体类型为“application / x-www-form-urlencoded”,这可能是您的问题所在,您编码的内容与您指定的媒体类型无关。

在不知道你的处理程序是什么样的情况下,我无法告诉你应该把它放在哪里。但是我可以告诉你,如果你要发送参数,它应该看起来像是媒体类型的key = value& key2 = value2,并且与URI中的内容无关(你的/一/二/三个例子) )。