PDA

View Full Version : Downloading a file with HTTP POST in Objective C




roeik
Nov 21, 2009, 04:42 AM
Hi,

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);
reqStream.Close();
StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
string Result = sr.ReadToEnd();

using (TextWriter tw = new StreamWriter("c:\\result.csv", true))
{
tw.Write(Result);
}
}
}
}



I cant figure out how to rewrite this code in objective C. Can someone help?

Thanks.
Roei



skunkworker
Nov 22, 2009, 02:03 PM
Hi,

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);
reqStream.Close();
StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
string Result = sr.ReadToEnd();

using (TextWriter tw = new StreamWriter("c:\\result.csv", true))
{
tw.Write(Result);
}
}
}
}



I cant figure out how to rewrite this code in objective C. Can someone help?

Thanks.
Roei


Look at NSURLConnection's initWithRequest.

roeik
Nov 24, 2009, 12:34 PM
Look at NSURLConnection's initWithRequest.

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

Thanks.

skunkworker
Nov 24, 2009, 12:39 PM
I looked at it and this is what I am using now.
Can you please try to be more specific?

Thanks.


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?

skunkworker
Nov 24, 2009, 12:40 PM
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

roeik
Nov 30, 2009, 06:18 AM
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. https://technet.rapaport.com/HTTP/Authenticate.aspx

Thanks for your help.

Roei.