Thursday, 8 December 2011

WCF service call from iPhone

POST_PARAMETERS_IN_WCF_BASED_SERVICE

1) CODE_IN_.NET

METHOD_SIGNATURE

[OperationContract]
[WebInvoke( Method="POST",

RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)
]



2) CODE_IN_OBJECTIVEC

TERMS_USED

kDevBaseURI="YOUR_REQUEST_PATH_URL"

-(NSString *) postRequest : (NSString *)requestMethod andRequestData : (NSMutableDictionary *) requestData{

NSString *requestString = [[NSString alloc] initWithFormat:@"%@/%@",kDevBaseURI,requestMethod];

NSLog(@"Request:%@",requestString);

NSURL *url=[NSURL URLWithString:requestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//IN_CASE_BACKEND_ACCEPTS_AS
//_REQUESTFORMAT_IS_JSON_FORMAT

NSString *postString = [NSString stringWithFormat:@"{\"name\":\"%@\",\"email\":\"%@\",\"password\":\"%@\"}",@"YOUR_NAME",@"YOUR_EMAIL_ID",@"YOUR_PASSWORD"];

//OTHERWISE
//NO_REQUEST_FORMAT

NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];

[jsonDict setObject:@"YOUR_NAME" forKey:@"name"];

[jsonDict setObject:@"YOUR_EMAIL_ID" forKey:@"email"];

[jsonDict setObject:@"YOUR_PASSWORD" forKey:@"password"];

NSString *postString = [requestData JSONRepresentation];
NSLog(@"POST_STRING:%@",postString);

//

NSData *postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSString *contentType = @"application/json; charset=utf-8";
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

unsigned long long postLength = postBody.length;

NSString *contentLength = [NSString stringWithFormat:@"%llu",postLength];
[request addValue:contentLength forHTTPHeaderField:@"Content-Length"];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];


NSError *error;
NSURLResponse *response;
NSData *webresponse = [[NSData data] retain];

webresponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *strJSON = [[NSString alloc]initWithBytes: [webresponse bytes]
length:[webresponse length] encoding:NSUTF8StringEncoding];


NSLog(@" here is web data : %@",strJSON);

return strJSON;

}


GET_PARAMETERS_IN_WCF_BASED

-(void) getRequest {

   NSString *requestString = [[NSString alloc] initWithFormat:@"%@/check?a=%@",kDevBaseURI,@"ABC"];
NSLog(@"Request check :%@",requestString);

NSString *responseString = [self stringWithUrl:[NSURL URLWithString:requestString]];
NSLog(@"Response :%@",responseString);

}

THIS_GENERIC_FUNCTION_USED_IN_BOTH_REQUEST(POST || GET)

- (NSString *)stringWithUrl:(NSURL *)url
{
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
    // Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
    NSString *result;
 
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                    returningResponse:&response
                                                error:&error];
 
  // Construct a String around the Data from the response
result = [[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding] autorelease];
 
    //NSLog(@"stringWithUrl urlRequest: %@ result: %@ response: %@ error: %@",urlRequest,result,response,error);
 
    return result;
}


No comments:

Post a Comment