【问题标题】:how to handle POST method with OpenRasta?如何使用 OpenRasta 处理 POST 方法?
【发布时间】:2010-10-22 12:44:28
【问题描述】:

我有一个简单的 OpenRasta 网络服务和一个用于网络服务的控制台客户端。

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

【问题讨论】:

  • 处理程序的代码会很有用。

标签: post openrasta


【解决方案1】:

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

如果不知道您的处理程序是什么样的,我无法告诉您应该在其中放入什么。但是,我可以告诉您,如果您要发送参数,则对于该媒体类型,它应该看起来像 key=value&key2=value2 ,并且与 URI 中的内容无关(您的 /one/two/three 示例)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多