WCF (Remote Server Returns Error: 400 Error request), wcf400
Similar issues include:
An error is returned when the POST method of the WCF-restful interface is called (the remote server returns a 400 Error request)
WCF Rest: using post instead of UriTemplate to pass Parameters solves the problem of HTTP400 and parameter ing
Wait!
For specific reasons refer to: Original: Reprint please indicate the source: http://www.cnblogs.com/sh91/p/3273072.html
Mark the cause of the problem as follows:
Server interface:
[WebInvoke (Method = "*", RequestFormat = WebMessageFormat. Json, ResponseFormat = WebMessageFormat. Json)]
[OperationContract]
String GetExptrainhist (string strjson); // The input parameter is of the string type.
Client call:
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (@ "http: // localhost: 8734/GetExptrainhist ");
Request. ContentType = "application/json ";
Request. Method = "POST ";
// ======= Test data ======= start ======================
ExpTrainHist_req t = new ExpTrainHist_req ();
T. account = "exp ";
T. fc = "expTrainHist_req ";
T. starttime = "13:00:00 ";
T. endtime = "14:04:51 ";
String data = Newtonsoft. Json. JsonConvert. SerializeObject (t );
// String inputString = "c901411 ";
// ======= End ================
String inputString = data;
// String inputString = "{\" IdNumbr \ ": \" 612523198308190014 \ ", \" StuId \ ": \" 901411 \ ", \" SchCode \": \ "1011099212 \", \ "TrainId \": \ "3 \", \ "StartTime \": \ "00:00:01 \", \ "\": \ "3000 \", \ "ScoreHis \": \ "10 \"}";
Byte [] byteArray = System. Text. Encoding. UTF8.GetBytes (inputString );
Request. ContentLength = byteArray. Length;
Stream rstream = request. GetRequestStream ();
Rstream. Write (byteArray, 0, byteArray. Length );
Rstream. Close ();
HttpWebResponse response = (HttpWebResponse) request. GetResponse (); // the following error is returned: the remote server returns a 400 Error request.
Stream stream = response. GetResponseStream ();
StreamReader SR = new StreamReader (stream );
String info = SR. ReadToEnd ();
String temp = Newtonsoft. Json. JsonConvert. DeserializeObject (info). ToString ();
SR. Dispose ();
==================================
Http://www.cnblogs.com/sh91/p/3273072.htmlblogs:
Note: The default JSON format is "". Therefore, "\" ss \ "" cannot be written as "ss". Otherwise, it is still caused by an HTTP400 error. Because the data is not recognized, splice the data into other bytes.
Change your input parameters:
// First, change the double quotation marks of the json string of data to single quotation marks, and then add the double quotation marks to the service. The problem is solved.
InputString = "\" "+ data. Replace (" \ "", "\ '") + "\""
Byte [] byteArray = System. Text. Encoding. UTF8.GetBytes (inputString );
Request. ContentLength = byteArray. Length;
Stream rstream = request. GetRequestStream ();
Rstream. Write (byteArray, 0, byteArray. Length );
Rstream. Close ();