0

这是我的 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<UserDTO>(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

<system.serviceModel>
    <services>
      <service name="VivuFace_V2.api.CloudCameraService" behaviorConfiguration="VivuFace_V2.api.CloudCameraServiceBehavior">
        <endpoint address="../CloudCameraService.svc"
            binding="webHttpBinding"
            contract="VivuFace_V2.api.ICloudCameraService"
            behaviorConfiguration="webBehaviour" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="VivuFace_V2.api.CloudCameraServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我正在使用下面的代码测试 POST Web 服务的 CloudCameraService.svc URL 中的 ResetPasswordAccount 方法:http://10.88.32.13:8082/api/CloudCameraService.svc/ResetPasswordAccount JSON 发送到 Web 服务:

{
  "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"}。我不知道为什么?

4

0 回答 0