-1

I want to use http Get and Post for getting the request and response of certain URL request,

But i dont know how to use them in objective c..

and Which one will come first Get or Post in establishment of connection.?

how to modify the content and post them back to the server..

Can any one please help me?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Raju Hegganni
  • 53
  • 2
  • 8

2 Answers2

1

for get use :

+(NSMutableURLRequest*)getURq_getansascreen:(NSString*)ws_name {

    NSLog(@"%@",ws_name);

    NSMutableURLRequest *urlReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:ws_name] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

    [urlReq addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [urlReq setHTTPMethod:@"GET"];

    return urlReq;
}

for post use :

+(NSMutableURLRequest*)postURq_getansascreen:(NSString*)ws_name :(NSString*)service {

        NSString *tempUrl = domainURL;

        NSString *msgLength = [NSString stringWithFormat:@"%d",[ws_name length]];
        NSMutableURLRequest *urlReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@Service=%@",tempUrl,service]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
        [urlReq addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [urlReq addValue:msgLength forHTTPHeaderField:@"Content-Length"];
        [urlReq setHTTPMethod:@"POST"];
        [urlReq setHTTPBody: [ws_name dataUsingEncoding:NSUTF8StringEncoding]];

        return urlReq;
    }

//Call this in view did load as `

WSPContinuous *wspcontinuous = [[WSPContinuous alloc] initWithRequestForThread:[webService getURq_getansascreen:[webService GetDetails:str_filter]] sel:@selector(WS_GetDetailsLoaded:) andHandler:self];`

//create class WSPContinuous and add these fns..

-(id)initWithRequestForThread:(NSMutableURLRequest*)urlRequest sel:(SEL)seletor andHandler:(NSObject*)handler {

    if (self=[super init]) {

        self.MainHandler = handler;
        self.targetSelector = seletor;
        self.urlReq = urlRequest;

        [self performSelectorOnMainThread:@selector(startParse) withObject:nil waitUntilDone:NO];
    }
    return (id)urlReq;
}

-(void)startParse{

    NSLog(@"URL CALLING    %@",urlReq.URL);

    con = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];
    if (con) {
        myWebData = [[NSMutableData data] retain];

        NSLog(@"myWebData old....%@",myWebData);
    }
    else {
        [self.MainHandler performSelectorOnMainThread:targetSelector withObject:nil waitUntilDone:NO];
    }
}

//-------------------------------connection-----------------

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [myWebData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [myWebData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    [self.MainHandler performSelectorOnMainThread:targetSelector withObject:nil waitUntilDone:NO];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSString *thexml = [[NSString alloc] initWithBytes:[myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];

    NSLog(@"xmlDictionary %@",thexml);

    [thexml release];

    NSError *parseError = nil;
    NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:myWebData error:&parseError];

    [AlertHandler hideAlert];

    [MainHandler performSelector:targetSelector withObject:xmlDictionary];
}
Justin Mathews
  • 512
  • 4
  • 18
  • can we write the above code in viewDidLoad method? and where will be response stored (response in HTML form) ? i want to c the response in html form.. – Raju Hegganni Feb 07 '13 at 05:05
  • can you send me the entire program for accessing a gmail(Get) page and Posting the userID and password to it without using the gmail UI.. and i want to Print the HTML response(content in response page) which i got in NSLog(). – Raju Hegganni Feb 07 '13 at 06:47
0

If you want to start, a better idea would be to do some reading on NSMutableURLRequest and related topics like NSURLConnection.

You get sample code everywhere. Just google it.

Google search -> objective c get and post

and First hit -> Tutorials for using HTTP POST and GET on the iPhone in Objective-C

Community
  • 1
  • 1
esh
  • 2,842
  • 5
  • 23
  • 39