Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 21, 2009, 05:42 AM   #1
roeik
macrumors member
 
Join Date: Dec 2008
Downloading a file with HTTP POST in Objective C

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#:

Code:
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
roeik is offline   0 Reply With Quote
Old Nov 22, 2009, 03:03 PM   #2
skunkworker
macrumors regular
 
Join Date: Sep 2007
Quote:
Originally Posted by roeik View Post
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#:

Code:
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.
skunkworker is offline   0 Reply With Quote
Old Nov 24, 2009, 01:34 PM   #3
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
Quote:
Originally Posted by skunkworker View Post
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.
roeik is offline   0 Reply With Quote
Old Nov 24, 2009, 01:39 PM   #4
skunkworker
macrumors regular
 
Join Date: Sep 2007
Quote:
Originally Posted by roeik View Post
I looked at it and this is what I am using now.
Can you please try to be more specific?

Thanks.
Code:
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 is offline   0 Reply With Quote
Old Nov 24, 2009, 01:40 PM   #5
skunkworker
macrumors regular
 
Join Date: Sep 2007
Also you need to implement the NSURLConnectionDelegate methods.

Code:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
skunkworker is offline   0 Reply With Quote
Old Nov 30, 2009, 07:18 AM   #6
roeik
Thread Starter
macrumors member
 
Join Date: Dec 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. https://technet.rapaport.com/HTTP/Authenticate.aspx

Thanks for your help.

Roei.

Last edited by roeik; Nov 30, 2009 at 07:27 AM.
roeik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Free File Downloading lillianmyoung MacBook Air 8 Jan 15, 2014 05:52 AM
Firefox downloading unknown file from googlesyndication.com ammemma Mac OS X 10.7 Lion 5 Mar 4, 2013 03:43 PM
new objective C-class won't create .h file OriginalJef iPhone/iPad Programming 2 Mar 3, 2013 04:24 PM
Files from file sharing not downloading mast3rumann iMac 0 Dec 4, 2012 12:37 PM
Trouble downloading a file bo-waleed Mac Basics and Help 4 Jun 30, 2012 06:07 AM

Forum Jump

All times are GMT -5. The time now is 05:21 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC