WCF (Remote Server Returns Error: 400 Error request), wcf400

Source: Internet
Author: User

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 ();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.