code for sending & recievieng the data to the server through Get and Post method in xcode


.h file


@interface ConnectionExampleViewController : UIViewController

@property (retain, nonatomic) NSURLConnection *connection;
@property (retain, nonatomic) IBOutlet UIButton *btn_post;
@property (retain, nonatomic) IBOutlet UIButton *btn_get;
@property (retain, nonatomic) NSMutableData *receivedData;

-(IBAction)btn_get_clicked:(id)sender;
-(IBAction)btn_post_clicked:(id)sender;

.m file

-(IBAction)btn_get_clicked:(id)sender
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    NSString *urlString = @"http://www.example.com/command=18&email=email&exchanges=hong%20kong%20Stock%20Exchange&instrumentclass=Stock&payforservice=YES&country=china";
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"GET"];
    NSString *type = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
    [request addValue:type forHTTPHeaderField: @"Type"];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Processed Value==>%@",returnData);
  }
-(IBAction)btn_post_clicked:(id)sender{
    [self.connection cancel];
        NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;
        NSURL *url = [NSURL URLWithString:@"http://www.example.com/command=18&email=email&exchanges=hong%20kong%20Stock%20Exchange&instrumentclass=Stock&payforservice=YES&country=china"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
    
    [request setHTTPMethod:@"GET"];
    //initialize a post data
    NSString *postData = @"";
    [request setValue:@"application/x-www-form-urlencoded;charset=utf-8"forHTTPHeaderField:@"Content-Type"];
   //set post data of request
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection;
    //start the connection
    [connection start];
    
}

Comments

Popular posts from this blog