2010-10-22 69 views
0

我有一个简单的OpenRasta web服务和Web服务的控制台客户端。如何使用OpenRasta处理POST方法?

使用GET方法是很容易 - 我定义得到OpenRasta,当客户端使用此代码,这一切工作正常

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 web服务中处理这个问题。如何在Openrasta中定义POST方法?有什么建议么?

+1

您的处理程序的代码将是有用的。 – 2010-10-22 13:19:57

回答

2

您提供的代码会发送“one/two/three”,并将其放在您的请求的内容中,并使用媒体类型“application/x-www-form-urlencoded”,这可能是您的问题出在哪里,因为您编码的内容与您指定的媒体类型无关。

不知道你的处理程序是什么样的,我不能告诉你你应该把它放在里面。但是我可以告诉你,如果你发送参数,它应该看起来像key = value & key2 = value2,与URI中的内容无关(你的/ one/two/three例子)。