Anfrage.ContentType = "application/json" gibt bad request " - Fehler auf WCF-Methode

Habe ich einen WCF-Dienst mithilfe von WCF REST Service Template 40(CS) und methodenkopf sieht wie folgt aus:

[WebInvoke(UriTemplate = "CTNotification", Method = "POST", ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json)]      

public string CTNotification(Stream contents)

und hier ist, wie ich es mit:

 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";

Es gibt schlechte Fehler 400. aber wenn es der content-Typ, plain, es funktioniert aber möchte ich weitergeben json gespeichert, die in json.txt. Bitte schlagen Sie mich, wie es zu tun ?

Dank.

Schreibe einen Kommentar