我使用WCF REST 服务模板 40(CS)创建了一个 WCF 服务 ,方法头如下所示:
[WebInvoke(UriTemplate = "CTNotification", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public string CTNotification(Stream contents)
这是我使用它的方式:
string url = ConfigurationManager.AppSettings["serviceUrl"];
string requestUrl = string.Format("{0}CTNotification", url);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
request.ContentType = "application/json";
//request.ContentType = "text/plain";
request.Timeout = 5000000;
byte[] fileToSend = File.ReadAllBytes(Server.MapPath("~/json.txt"));
request.ContentLength = fileToSend.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Send the file as body request.
requestStream.Write(fileToSend, 0, fileToSend.Length);
requestStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
Label1.Text = "file uploaded successfully";
它给出错误 400。但如果它使内容类型简单,它可以工作,但我想传递存储在 json.txt 中的 json。请建议我怎么做?
谢谢。