在IIS中托管时支持Web服务

这是我的ICloudCameraService.cs

[OperationContract] // Reset account (password default) [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "ResetPasswordAccount")] WsResult ResetPasswordAccount(Stream jsonDataStream); 

在这里,我的WsResult.cs

 [DataContract] [Serializable] public class WsResult { [DataMember] public string result { get; set; } } 

这是我的CloudCameraService.svc

 public WsResult ResetPasswordAccount(Stream jsonDataStream) { try { // Read in our Stream into a string... StreamReader reader = new StreamReader(jsonDataStream); string JSONdata = reader.ReadToEnd(); // ..then convert the string into a single "wsOrder" record. UserDTO user = jss.Deserialize(JSONdata); if (user == null) { // Error: chuỗi JSON đưa vào không đúng định dạng (JSON isn't correct format) result.result = "2"; } else if (!user.Key.Equals(Global.SecretKey)) (secret key not match) { result.result = "5"; } else if (string.IsNullOrEmpty(user.Username)) { // Error: Các field bắt buộc đang để trống hoặc không nhập vào (this field is empty) result.result = "4"; } else if (!userBus.CheckExistUser(user.Username)) { result.result = "3"; } else { bool check = userBus.ResetPasswordForUser(user.Username); // 0: thành công (success), 3: thất bại(failed) result.result = check ? "0" : "1"; } } catch (Exception) { // Error: chuỗi JSON không đúng định dạng(JSON isn't correct format) result.result = "2"; } return result; } 

这是我的Web.config

                      

我正在使用Cloud CameraService.svc中的测试方法ResetPasswordAccount来测试POST Web服务的URL: http://10.88.32.13:8082/api/CloudCameraService.svc/ResetPasswordAccount ://10.88.32.13:8082 / api / CloudCameraService.svc / ResetPasswordAccount要发送到Web服务的JSON:

 { "Key": "b571af9b-f425-4a25-8ee7-76dcd3a9eaf7", "CameraID": 743 } 

这是测试的代码:

 protected void btnCallPOSTwebService_Click(object sender, EventArgs e) { // The user has clicked on the "Call POST web service" button try { string WebServiceURL = tbWebServiceURL.Text; // Convert our JSON in into bytes using ascii encoding ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(tbJSONdata.Text); // HttpWebRequest HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(WebServiceURL); webrequest.Method = "POST"; webrequest.ContentType = "application/x-www-form-urlencoded"; webrequest.ContentLength = data.Length; // Get stream data out of webrequest object Stream newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); // Declare & read the response from service HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); // Fetch the response from the POST web service //Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream()); string result = loResponseStream.ReadToEnd(); loResponseStream.Close(); webresponse.Close(); txtResult.Text = result; } catch (Exception ex) { txtResult.Text = "An exception was thrown: " + ex.Message; } } 

当我从localhost运行时,一切正常,但是当我在IIS 7中托管和运行时,结果总是返回{“result”:“2”}。 我不知道原因是什么?