我有一个基于REST的WCF网络服务;
合同是:
[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获得响应,但它确实有效。我使用wireshark来点击消息,我可以看到一些重要的事情:
1)发送xml 2)内容类型
由此我构建了以下请求逻辑:
//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();
然而,当我点击说:
dataStream = response.GetResponseStream();
我收到以下错误:
“远程服务器返回错误:(400)错误请求”
有人可以帮助我完成我需要做的事情,因为我需要能够告诉人们如何手动创建POST请求以与此基于REST的服务进行交互。
任何非常感谢的帮助都没有真正看到我还能尝试的其他内容。
答案 0 :(得分:1)
如果内容类型表明您正在发送XML,那么您不应该转义XML以将其发送到服务 - 至少不是包装消息;如果你有一些字符需要在内容(文本)中转义,那么你需要逃避它们。将postData更改为下面的行,它应该可以工作。
string postData = "<EchoWithPost xmlns=\"http://tempuri.org\"><message>Hello & goodbye</message></EchoWithPost>";
答案 1 :(得分: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);
的web.config:
答案 2 :(得分:1)
下载此工具http://www.fiddler2.com/fiddler2/
尝试调用rest方法并在fiddler中查看原始数据(请求响应),您将获得有关错误的确切信息
答案 3 :(得分:0)
好Pete2k,我想说如果不运行你的服务,可能需要一些时间来重建这个。您使用的是WCF 4.0 REST项目吗?如果是这样,它有一个帮助页面,可以显示请求数据的样子。