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 Web サービスでこれを処理する方法がわかりません。Openrasta で POST メソッドを定義するにはどうすればよいですか? 助言がありますか?

4

1 に答える 1

2

あなたが提供するコードは「one/two/three」を送信し、それを「application/x-www-form-urlencoded」のメディアタイプでリクエストのコンテンツに入れます。おそらく、それが問題の原因です。エンコードされた ve は、指定したメディア タイプとは関係ありません。

ハンドラーがどのように見えるかを知らなければ、ハンドラーに何を入れるべきかわかりません。ただし、パラメーターを送信している場合、そのメディアタイプの key=value&key2=value2 のように見える必要があり、URI に含まれるものとは何の関係もありません (/one/two/three の例)。

于 2010-11-08T21:28:01.193 に答える