Showing posts with label POST URL. Show all posts
Showing posts with label POST URL. Show all posts

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;
}


Monday, 8 August 2011

IMAGE ( BASE-64 STRING ) POST URL IN OBJECTIVE C



Steps:

1.Add Cryptor library for encryption/decryption
2.UIImageView * imageView;

/*--------------------------------------Code Starts here-------------------------- -------------*/
     Cryptor *crypt = [[Cryptor alloc] init];
    NSData *imgData =[[NSData alloc]initWithData:UIImagePNGRepresentation(self.imageView.image)];
    NSString *imageString = (NSString *)[crypt base64Encoding:imgData];
    
    
    //Your WebMethod =>
*webMethodname 
*inputParameters -> Method expects parameter.
 *kURI-> Path where you call webservices .

    NSString *stringURI= [NSString stringWithFormat:@"%@?method=WBMETHODNAME & inputParameters=%@,kURI,inputfields];
    stringURI = [stringURI stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    

    NSURL *url=[NSURL URLWithString:stringURI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSString *postString = [NSString stringWithFormat:@"image=%@",[self encodeString:imageString]];
    
    NSData *postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *contentType = @"application/x-www-form-urlencoded; 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 *strXML = [[NSString alloc]initWithBytes: [webresponse bytes]
                                               length:[webresponse length] encoding:NSUTF8StringEncoding];
    NSLog(@"RESPONSE  here is web data : %@",strXML);
    
}

- (NSString *)encodeString:(NSString *)string {
    
    NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease]);
    
    return newString;
    
}