如何使用 OpenRasta 处理 POST 方法?

发布于 2024-09-28 07:44:43 字数 1655 浏览 2 评论 0原文

我有一个简单的 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 方法?有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-10-05 07:44:43

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

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

The code you provide sends "one/two/three" and put it in the content of your request with a media type of "application/x-www-form-urlencoded", that's probably where your problem comes from, as what you've encoded has nothing to do with the media type you've specified.

Without knowing what your handler looks like, I can't tell you what you should put in it. I can however tell you that if you're sending parameters, it should look like key=value&key2=value2 for that media type, and has nothing to do with what would go in the URI (your /one/two/three example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文