Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 21, 2009, 04: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, 02: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, 12: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, 12: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, 12: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, 06: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 06: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
sharing files with the guest account iamrawr Mac Basics and Help 1 Feb 5, 2011 05:13 PM
Delete a file with the keyboard in Finder? askfareed Mac Basics and Help 22 Sep 17, 2010 09:53 PM
Reading Word files with hyperlinks or embedded objects cjane iPod touch 5 Jan 8, 2010 06:31 AM
File with odd characters in its name wont delete from trash ray_uk OS X 1 Nov 7, 2008 09:04 AM
How can I download .w3c file with Safari? SLJ Mac Applications and Mac App Store 9 Sep 7, 2003 03:21 AM


All times are GMT -5. The time now is 07:56 PM.

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

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