【发布时间】:2012-02-04 15:21:15
【问题描述】:
我需要创建一个带有 4 个字符串参数的 POST 方法的 REST 服务
我定义如下:
[WebInvoke(UriTemplate = "/", Method = "POST", BodyStyle= WebMessageBodyStyle.WrappedRequest)]
public Stream ProcessPost(string p1, string p2, string p3, string p4)
{
return Execute(p1, p2, p3, p4);
}
我想用如下代码调用它:
string result = null;
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string paramz = string.Format("p1={0}&p2={1}&p3={2}&p4={3}",
HttpUtility.UrlEncode("str1"),
HttpUtility.UrlEncode("str2"),
HttpUtility.UrlEncode("str3"),
HttpUtility.UrlEncode("str4")
);
// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(paramz);
req.ContentLength = postData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Pick up the response:
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
但是,客户端代码返回代码 400:错误请求
我做错了什么?
谢谢
【问题讨论】:
-
尝试打开 WCF 跟踪以查看问题所在,但我认为服务需要 XML