2013-04-17 27 views
1

有人請讓我知道如何使用目標c傳遞參數給WCF函數?
1.我用C#開發WCF。
2. WCF終點如下。將參數傳遞給WCF函數使用Objective c

<system.serviceModel> 
    <services> 
    <service name="iAppServ.Service1" behaviorConfiguration="ServBehave"> 
     <!--Endpoint for SOAP--> 
     <endpoint address="soapService" binding="basicHttpBinding"  contract="iAppServ.IService1"/> 
     <!--Endpoint for REST--> 
     <endpoint address="XMLService" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="iAppServ.IService1"/> 
    </service> 
    </services> 
    <bindings> 
    <webHttpBinding> 
     <binding crossDomainScriptAccessEnabled="True" name="webHttpBinding"> 
     </binding> 
    </webHttpBinding> 
    </bindings> 
    <behaviors> 
    <serviceBehaviors> 
     <behavior name="ServBehave" > 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <!--Behavior for the REST endpoint for Help enability--> 
     <behavior name="restPoxBehavior" > 
     <webHttp helpEnabled="true" /> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
  1. 我想傳遞一個SEARCHTEXT消費 「SearchUserData」 功能。

  2. WCF將返回XML數據。

+0

你有什麼試過的?你甚至搜索相關的帖子或主題? [在iPhone上使用Objective-C來使用WCF Web服務](http://stackoverflow.com/a/988884/745969)。 – Tim

+0

還有更多 - [用目標c調用WCF](https://www.google.com/search?source=ig&rlz=1G1ACGW_ENUS358&q=Calling+WCF+with+object+c&oq=Calling+WCF+with+object+ C&gs_l = igoogle.3 ... 1428793.1432218.0.1432464.25.22.0.2.1.0.206.2424.14j7j1.22.0 ... 0.0 ... 1ac.1.HMeJwc4MgXI#RLZ = 1G1ACGW_ENUS358&sclient = PSY-AB&q =主叫+ WCF +與+客觀+ C&OQ =致電+ WCF +與+目標+ C&gs_l = serp.3 ... 111342.111624.0.111793.3.3.0.0.0.1.129.332.1j2.3.0.ckwqrh..0.0 ... 1.1.9.psy-AB .Y7OtCrO6dyM&pbx = 1&bav = on.2,or.r_cp.r_qf。&bvm = bv.45368065,d.cGE&fp = d9b9f9a5509b56c8&biw = 1366&bih = 703) – Tim

+0

非常感謝,Tim。我已經嘗試從您在此處提到的鏈接中搜索內容。他們中的大多數都使用Web服務而不是WCF。其中一些正在使用「獲取」方法。我是新來的客觀的C.你能請說出這個話題嗎? –

回答

1

此代碼正常工作。

NSString *soapMessage = [NSString stringWithFormat:@"<SearchUserData xmlns=\"http://tempuri.org/\"><SearchText>"]; 

soapMessage=[soapMessage stringByAppendingString:searchBar.text]; 
soapMessage=[soapMessage stringByAppendingString:@"</SearchText> </SearchUserData> "]; 

NSURL *url = [NSURL URLWithString:@"http://your server.svc/XMLService/SearchUserData"]; 

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 

[theRequest addValue: @"text/xml; charset=utf-16" forHTTPHeaderField:@"Content-Type"]; 
[theRequest addValue: @"http://tempuri.org/IService1/SearchUserData" forHTTPHeaderField:@"SOAPAction"]; 
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
// NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if(theConnection) { 
    NSError *WSerror; 
    NSURLResponse *WSresponse; 
    NSString *theXml; 
    NSData *myData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&WSresponse error:&WSerror]; 
    theXml = [[NSString alloc]initWithBytes:[myData bytes] length:[myData length] encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",theXml); 



} 
else { 

    NSLog(@"theConnection is NULL"); 
}