0

我在服务器端使用了这段代码,

[OperationContract]        
        [WebInvoke(Method = "POST", UriTemplate = "/UploadData?Name={Name}&Email={Email}&ContactNumber={ContactNumber}&DeviceModel={DeviceModel}&Problem={Problem}&Besttimetocontact={Besttimetocontact}",        
            BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
        NewClsMessage UploadData(string Name, string Email, string ContactNumber, string DeviceModel, string Problem, string Besttimetocontact);

 public NewClsMessage UploadData(string Name, string Email, string ContactNumber, string DeviceModel, string Problem, string Besttimetocontact)
{
            NewClsMessage ObjNewClsMessage = new NewClsMessage();
            try
            {
  str_value = "insert into Estimate(Name,Email,ContactNumber,DeviceModel,Problem,Besttimetocontact) values('" + Name + "','" + Email + "','" + ContactNumber + "','" + DeviceModel + "','" + Problem + "', '" + Besttimetocontact + "')";                 

                OpenConnection();
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = str_value;
                value = sqlCmd.ExecuteNonQuery();
                if (value > 0)
                {
                    ObjNewClsMessage = new NewClsMessage();
                    ObjNewClsMessage.Result = "1";
                    ObjNewClsMessage.Message = "send Successfully";
                }
                else
                {
                    ObjNewClsMessage = new NewClsMessage();
                    ObjNewClsMessage.Result = "0";
                    ObjNewClsMessage.Message = "Free Estimate not send Successfully";
                }
}

 catch (Exception ex)
            {
            }
            return ObjNewClsMessage;

客户端代码是,

WebClient webClient = new WebClient();
            webClient.Headers[HttpRequestHeader.ContentType] = "application/xml";
            //  var uri = new Uri("localhost:55666/WcfPostMethod/Service.svc/UploadXml");
            var uri = new Uri("http://localhost:50546/Service1.svc/UploadData");
            StringBuilder postData = new StringBuilder();
            postData.AppendFormat("?{0}={1}", "Name", HttpUtility.UrlEncode("hi"));
            postData.AppendFormat("&{0}={1}", "Email", HttpUtility.UrlEncode("hi@gmail.com"));
            postData.AppendFormat("&{0}={1}", "ContactNumber", HttpUtility.UrlEncode("56898"));
            postData.AppendFormat("&{0}={1}", "DeviceModel", HttpUtility.UrlEncode("E4376"));
            postData.AppendFormat("&{0}={1}", "Problem", HttpUtility.UrlEncode("Repair"));
            postData.AppendFormat("&{0}={1}", "Besttimetocontact", HttpUtility.UrlEncode("9am"));            
            webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            webClient.UploadStringAsync(uri, "POST", postData.ToString());

在这里,我在服务器端收到错误..在客户端应用程序中发布数据意味着,在服务器端数据获取空(null)值。如何解决这个问题?

4

1 回答 1

0

你能告诉我们你的 wcf 服务在没有你的客户端的情况下是否可以工作?

这是一个不错的工具(Storm 或 WCF Storm)

风暴

你甚至可以找到一个样本:

使用 Windows Phone 7 应用程序使用 WCF 服务

于 2013-01-09T18:32:55.463 回答