2011-06-21 6 views
1

私はRESTベースのWCF Webサービスを持っています。誰も私がこの残りのベースのwebserviceのhttp要求を手動で構築する手助けができますか?

契約は次のとおりです。

[OperationContract] 
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)] 
string EchoWithPost(string message); 

メッセージは次のとおりです。

public string EchoWithPost(string s) 
{ 
    return "ECHO with POST : You said " + s; 
} 

私はPOSTを経由して応答を取得するためにウェブチャネルファクトリを使用し、それが動作します。

 //5) manually post to the REST service 
     //Create a request using a URL that can receive a post. 
     WebRequest request = WebRequest.Create(urlOfService + "/rest/EchoWithPOST"); 
     // Set the Method property of the request to POST. 
     request.Method = "POST"; 
     // Create POST data and convert it to a byte array. 


     string postData = "<EchoWithPost xmlns="http://tempuri.org"><message>Hello</message><EchoWithPost>"; 



     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/xml"; 
     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 
     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 
:私は、次のリクエストのロジックを構築した。このことから)コンテンツタイプ

1)、そのXMLが 2に送信されます。私は、メッセージをタップするのwiresharkを使用し、私はいくつかの重要な事柄を見ることができます

は、しかし、私は言うラインを打ったとき:

dataStream = response.GetResponseStream();

私は次のエラーを取得する:

"リモートサーバーからエラーが返されました:(400)Bad Request"

私が必要とするものを手伝ってもらえますか?人と人との対話のためのPOSTリクエストを手動で作成する方法を教えてもらえますか?このRESTベースのサービス。

本当にありがとうございました。他に何を試してみるか分かりません。

答えて

1

私はいくつか小さな変更を加えましたので、私は全体を投稿します。うまくいけばそれはあなたのために働く。また、デシリアライズを追加していないので、HTTP 400エラーを過ぎてしまえばそれに取り組むことができます。

これらの状況をデバッグするのに役立つ素晴らしいツールはSoapUIです。 「Web TestCase」をセットアップするだけで、独自のPOSTリクエストを作成したり、前後するデータを監視することができます。

-Vito

インタフェース:

[OperationContract] 
    [WebInvoke(UriTemplate = "EchoWithPost", Method="POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] 
    string EchoWithPost(string message); 

サービス:

public string EchoWithPost(string s) 
    { 
     return "ECHO with POST : You said " + s; 
    } 

クライアント:

string urlOfService = "http://somewhere.com/RestService.svc/EchoWithPost"; 
string postData = "<EchoWithPost xmlns=\"http://tempuri.org/\"><message>Vito</message></EchoWithPost>"; 
byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

WebRequest request = WebRequest.Create(urlOfService); 
request.Method = "POST"; 
request.ContentType = "application/xml;"; 
request.ContentLength = byteArray.Length; 
Stream dataStream = request.GetRequestStream(); 
dataStream.Write(byteArray, 0, byteArray.Length); 
dataStream.Close(); 

WebResponse webResponse = request.GetResponse(); 

// Output raw string result 
string rawStringResult = new StreamReader(webResponse.GetResponseStream()).ReadToEnd(); 
HttpContext.Current.Response.Write("\r\n" + rawStringResult); 

ウェブ。設定:

+0

こんにちはヴィトー、これは基本的に私は両方のコードセクションを見て、私が取る必要がすべてのxmlをきれいにしたので、ありがとう。私はSOAP UIを取得し、それを使って遊びます – Exitos

1

コンテンツタイプによってXMLを送信していると表示された場合、XMLをエスケープしてサービスに送信しないでください。少なくともメッセージの折り返しはありません。内容(テキスト)でエスケープする必要のある文字がある場合は、それらをエスケープする必要があります。 postDataを下の行に変更してください。これはうまくいくはずです。

string postData = "<EchoWithPost xmlns=\"http://tempuri.org\"><message>Hello &amp; goodbye</message></EchoWithPost>"; 
+0

こんにちはCarols私はエスケープしてエスケープしていませんでした。実際の問題が何かを見つけたら、あなたの考えを念頭に置いてください。 – Exitos

0

よくPete2k、私はあなたのサービスを実行しないと、これを再構築するには時間がかかることがあります。 WCF 4.0 RESTプロジェクトを使用していますか?もしそうなら、それはヘルプページを持っています。このページには、リクエストデータの外観が表示されます。

1

ダウンロードこのツールhttp://www.fiddler2.com/fiddler2/

残りの方法を呼び出して、フィドラーの生データ(要求応答)を表示しようとすると、エラーの正確な情報が得られます。

関連する問題