0

我有一个简单的 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 webservice 中处理这个问题。如何在 Openrasta 中定义 POST 方法?有什么建议么?

4

1 回答 1

2

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).

于 2010-11-08T21:28:01.193 回答