shoutcast pls forbidden for iPhone programmatically? - iphone

I have been trying to access the pls file data from shoutcast for some testing but the response seems to be forbidden and I am getting 403 as response.
here is the code
NSURL *myurl = [NSURL URLWithString:#"http://yp.shoutcast.com/sbin/tunein-station.pls?id=9944"] ;
//Accept:*/*
NSMutableURLRequest *myrequest = [[NSMutableURLRequest alloc]initWithURL:myurl];
[myrequest setValue:#"*/*" forHTTPHeaderField:#"Accept"];
//NSURL *myurl = [NSURL URLWithString:#"http://www.google.com"];
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:myrequest delegate:self];
On the response, it was showing a 403 and no data is received. I tried to check the content-type and it was showing audio/x-scpls

In case of disallowing NSURLRequests, changing the submitted user agent should avoid the 403 response:
[myRequest setValue:userAgent forHTTPHeaderField:#"useragent"];

Related

What I did is correct or not? When I send an audio file to server in NSData

I am recording audio in a .wav format and converting the .wav1 file to NSData and sending to server.
Recorded path is:
file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav
I am sending to the server using:
audioURL=#"file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav";
NSURL *url=[NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod: #"POST"];
[request setValue: #"multipart/form-data" forHTTPHeaderField: #"Content-Type"];
NSData *audiodata = [NSData dataWithContentsOfURL:audioURL];
NSMutableData *highScoreData = [NSMutableData data];
[highScoreData appendData:audiodata];
[request setHTTPBody:highScoreData];
nsurlConnection = [[NSURLConnection alloc] initWithRequest: request
delegate: self];
When I play this back it gives me the right recorded voice. However, when I play the same recorded voice from the server it says "quotation" instead of the actual recorded voice.
My full code of how I did audio recording and how i send the audio can be found here.
Please tell me if the way I did this(i.e, to sending to server) is correct or not?
Multipart requests don't work that way. You need boundaries to differ between different data. Please check: http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
For the solution, I use AFNetworking (https://github.com/AFNetworking/AFNetworking).
The AFHTTPClient can create multipart requests for you.
Check: http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:
Here is the code I use to upload. The header part depends on how your server handles this.
AsiFormdata is more proper here.
NSString* fileString;
fileString = [[self documentsPath]
stringByAppendingPathComponent:#"testcombine.m4a"];
NSData* songData=[NSData dataWithContentsOfFile:fileString];
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://%#/UsingWebServer2/UploadServlet",RemoteEndpoint]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:songData withFileName:#"upload_test.m4a" andContentType:#"audio/m4a" forKey:#"file"];
//[request appendPostData:[Jstring dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
If you want to send the data via multipart/form-data, use ASIFormDataRequest. Find documentation here for more reference.

error.USER_REQUIRED generated when attempting to submit Reddit link in objective-c

I am having a small Reddit API problem here. I am trying to follow the reddit api, as outlined here:
https://github.com/reddit/reddit/wiki/API
Logging in using a simple NSMutableURLRequest is not a problem:
NSString *user = [[NSString alloc]initWithString:[userFld text]];
NSString *passwd = [[NSString alloc]initWithString:[passwordFld text]];
NSString *urlString = [NSString stringWithFormat:#"http://www.reddit.com/api/login/username/?user=%#&passwd=%#",
[user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[passwd stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Which gives me a result from which I extract the user mod-hash:
4029916%2C2010-04-30T22%3A51%3A52%2C1243925043100000000000000000000000000000
Next I am trying to post, using:
NSString *redditUrlString = [NSString stringWithFormat:
#"http://www.reddit.com/api/submit/?uh=%#&kind=link&url=%#&sr=%#&title=%#&r=%#&api_type=json",
[appDelegate globalUhString],
#"www.google.com",
#"funny",
#"Google.com",
#"funny"];
NSURL *url = [NSURL URLWithString:redditUrlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
However, when I extract the JSON response form the data received from this connection, I always receive the error below no matter what I use as the mod-hash:
[".error.USER_REQUIRED"]
Can someone explain what I have done incorrectly/how I can fix it?
It looks like you've not passing in the reddit_session cookie along with the uh parameter. The api documentation seems to explain the reason for receiving the error. According to this answer, it looks like you may need to issue the code:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
in the applicationDidBecomeActive handler, as it may be rejecting the cookies due to the CookieAcceptPolicy being shared amongst all applications.

ASIHTTPRequest setPostValue iPhone

I'm accessing a PHP script on a server.
The request URL is like this:
example.com/cgi-bin/getEvent.cgi?EID=19573
When I put in the request via a browser, I get back my expected results.
However when I use the ASIHTTP Form request, I'm getting back a result
like the EID isn't being passed via HTTP.
NSString *eventID = #"19573";
NSString * const EVENT_URL = #"http://example.com/cgi-bin/getEvent.cgi";
-(void)callWebservice
{
NSURL *url = [NSURL URLWithString:EVENT_URL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:eventID forKey:#"EID"];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDelegate:self];
[request startAsynchronous];
}
Anyone know of a method to see the full URL being requested?
Or have any clue why this wouldn't be working?
Thanks in advance.
My guess is that your PHP script is expecting the parameter to be sent on the query string (i.e. as a GET request) rather than as a POST parameter.
If that's the case, you can fix it be sending your request as follows:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#?EID=%#",EVENT_URL,eventID]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

iPhone & Google Places API

I am using Google place API in my application. Well, its working fine and giving response for some places. But for some places, its giving "ZERO RESULTS".
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%#&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect= [[NSURLConnection alloc]initWithRequest:request delegate:self];
Can anyone give me an idea where I am getting wrong?
If the location name is not valid it will give you zero results & in some cases because of extra spaces in the string you will get URL as null. So update your code as I mentioned below and check it out.
NSString *googleApi = [NSString stringWithFormat:#"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%#&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text];
NSString *finalURL = [googleApi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:finalURL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect= [[NSURLConnection alloc]initWithRequest:request delegate:self];
It may work for you.

NSMutableURLRequest loses session information

I have an iphone app that I'm trying to get to talk to a rails back end. I'm using NSMutableURLRequest to pull data back and forth. All the calls work fine on GET requests but when I need to post data, my rails app can't seem to find the session. I've posted the code below for both a get and a POST request.
This is the POST request:
//Set up the URL
NSString *url_string = [NSString stringWithFormat:#"https://testserver.example.com/players.xml"];
NSURL *url = [NSURL URLWithString:url_string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
//To create an object in rails
//We have to use a post request
////This is the format when using xml
NSString *requestString = [[NSString alloc] initWithFormat:#"<player><team_game_id>%#</team_game_id><person_id>%#</person_id></player>", game, person];
NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestData];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
//For some reason rails will not take application/xml
[request setValue:#"application/xml" forHTTPHeaderField:#"content-type"];
The get request is:
NSString *url_string = [NSString stringWithFormat:#"https://testserver.example.com/people/find_by_passport?passport=%i", passport];
passportString = [[NSMutableString alloc] initWithFormat:#"%i", passport];
NSLog(#"The passport string is %#", passportString);
NSLog(url_string, nil);
NSURL *url = [NSURL URLWithString:url_string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
I am at my wits end here trying to find out whats going on so any help would be GREATLY appreciated.
Faced the same issue. Parse the response headers for the Set-Cookie header, extract the cookies by hand, pass them back as Cookie: header on subsequent requests.
If this is just about sessions in their simplest, you don't have to worry about persistence, HTTPS, domains/paths and such.
EDIT: I found class NSHTTPCookieStorage. See if it can be of help...