Is there an easier way to do an HTTP POST

Discussion in 'iOS Programming' started by beachdog, Oct 25, 2008.

  1. beachdog macrumors member

    Joined:
    Aug 10, 2008
    #1
    Currently I'm having to construct the multi-part body myself (see example code below). I'm surprised that there isn't an easier way to do this...or is there?

    Code:
    /* construct body of the request */
    NSString* boundary = [NSString stringWithString:@"---28461D3FG"];
    NSMutableData* data = [NSMutableData data];
    [data appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"subscriberid\"\r\n\r\n%@",strSubId] dataUsingEncoding:NSASCIIStringEncoding]];
    [data appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"greetingid\"\r\n\r\n%@",strGreetingId] dataUsingEncoding:NSASCIIStringEncoding]];
    /* ...etc */
    
    NSURL* url = [NSURL URLWithString:@"http://216.41.17.74/uploaGreetingRequest.jsp"];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [data length]]  forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:data];
    
     
  2. hchung macrumors 6502a

    Joined:
    Oct 2, 2008
    #2

Share This Page