Downloading a file with HTTP POST in Objective C

Discussion in 'iOS Programming' started by roeik, Nov 21, 2009.

  1. macrumors member

    Dec 25, 2008

    I have an iPhone app in which the user downloads new data once a week from external server. The company that provides the data recently changed the authentication method to POST.

    So far I was using NSUrlConnection and NSUrlReqeust, and the delegate method didReceiveAuthenticationChallenge to enter the user name and password and then download the file (Using NSData to write the file).

    However, now that they changed the authentication method I can't figure out what classes to use to download the files. They gave the following example in C#:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    namespace TechnetSamples
    class Program
    static void Main(string[] args)
    string URL = "FILE_URL_PATH";
    System.Net.WebRequest webRequest = System.Net.WebRequest.Create(URL);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    Stream reqStream = webRequest.GetRequestStream();
    string postData = "username=YourUser&password=YourPassword";
    byte[] postArray = Encoding.ASCII.GetBytes(postData);
    reqStream.Write(postArray, 0, postArray.Length);
    StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
    string Result = sr.ReadToEnd();
    using (TextWriter tw = new StreamWriter("c:\\result.csv", true))
    I cant figure out how to rewrite this code in objective C. Can someone help?

  2. macrumors regular

    Sep 9, 2007

    Look at NSURLConnection's initWithRequest.
  3. thread starter macrumors member

    Dec 25, 2008
    I looked at it and this is what I am using now.
    Can you please try to be more specific?

  4. macrumors regular

    Sep 9, 2007
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[messageString dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection* connection = [[[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO] autorelease];
    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    And what do you mean, this is what im using now?
  5. macrumors regular

    Sep 9, 2007
    Also you need to implement the NSURLConnectionDelegate methods.

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  6. thread starter macrumors member

    Dec 25, 2008
    Ok. Sorry for not being clear. Let me try to explain it better.

    Right now I am using NSURLConnection using all the conventional delegate methods. I am using the delegate method didReceiveAuthenticationChallenge to enter the user's username and password and it's working perfectly.

    However, the company that provides the data will change the authentication method starting January first. More specifically, You can only send your credentials over HTTPS in POST data. Once sent you can choose to use the authentication ticket for further requests, or send your username and password in each request.

    I don't understand how I am supposed to work with the "ticket" using NSURLConnection. They gave the example I posted above in C#, but I don't really understand it and I don't know how to convert it to objective C.

    This is the page where they issue the ticket.

    Thanks for your help.


Share This Page