0
我已經使用在服務器端的代碼,如何在windows phone7應用程序中使用wcf服務?
[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("[email protected]"));
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());
在這裏,我得到的服務器錯誤side..while張貼在客戶端應用程序中的數據意思是在服務器端數據獲取空(null)值。如何解決這個問題?