Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

roeik

macrumors member
Original poster
Dec 25, 2008
80
0
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
 

skunkworker

macrumors regular
Sep 9, 2007
182
20
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

macrumors regular
Sep 9, 2007
182
20
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

macrumors regular
Sep 9, 2007
182
20
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
 

roeik

macrumors member
Original poster
Dec 25, 2008
80
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.