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.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ihrem service gibt 400-Fehler, weil die Daten, die Sie auf der Durchreise sind-der Dienst ist nicht im JSON-format. Sie haben dekoriert Ihr Betrieb den Vertrag mit
RequestFormat = WebMessageFormat.Json
so ist es nur akzeptieren, Daten im JSON-format.Sie machen die Anfrage an den service mit
Stream
Daten und deren MIME-Typ"text/plain" and "application/octet-stream"
. Senden JSON-der service, die gespeichert in der Datei, die Sie brauchen, um unten Veränderungen in Ihrem service als auch als client:Service:
Client:
Hoffe das kann dir helfen.