ios中調用WCF


例子比較簡單 記錄下思路

1、接口中定義 實體和方法聲明

        //登錄信息
        [OperationContract]
        [WebInvoke(UriTemplate = "LogInf/{name}/{pwd}", Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
        LogInf GetLogInf(string name, string pwd);

 

   2  //數據交換實體類

  [DataContract]  
    public class LogInf
    {

        [DataMember]
        public string UserId { get; set; }


        [DataMember]
        public bool LogTag { get; set; }

        [DataMember]
        public string errMsg { get; set; }


    }

 

3服務實現

//簡單測試

 public LogInf GetLogInf(string name, string pwd)
        {
            LogInf loginf = new LogInf();
            if (name == "111" && pwd == "111")
            {
                

                loginf.UserId = "100";
                loginf.LogTag = true;
                loginf.errMsg = "成功";

            }
            else
            {
                loginf.UserId = "0";
                loginf.LogTag = false;
                loginf.errMsg = "驗證失敗";
            }
            return loginf;
        }

 

4在web.config中將綁定方式改成webHttpBinding

binding="webHttpBinding"

5 調用

      NSURL *url = [NSURL URLWithString:@http://192.268.0.11:9422/Service1.svc/LogInf/111/111];  

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  

    [request setRequestMethod:@"POST"];  

   [request startSynchronous];  

    NSError *error = [request error];  

   if (!error) {  

       NSString *response = [request responseString];  

       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"測試" 

                                                            message:response  

                                                           delegate:nil  

                                                  cancelButtonTitle:@"OK" 

                                                 otherButtonTitles:nil];   

       [alertView show];  

       [alertView release];  

    }  

 

6返回

{"LogTag":true,"UserId":"100","errMsg":"成功"}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM