iphone posting data to a url - iphone

This is the string I need to post http://www.mysite.com/save?details={"name"}
Iam trying the following code;
NSString *post =[NSString stringWithFormat:#"details={\"name\"}"];
NSString *hostStr = #"http://www.mysite.com/save?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
but the serverOutput always = null, unless i do just {name}. but i need the qutoes in there :(

You should convert everythign to valid HTTP symbols.
e.g.:
hostStr = [hostStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
...my guess is that your webserver is refusing your invalid URL.

wrong ..
NSString *post =[NSString stringWithFormat:#"details={\"name\"}"];
Correct:
NSString *post =[NSString stringWithString:#"details={\"name\"}"];

Related

Convert this url to base 64

I tried this way but it's not working.
This is the url to convert to base 64
NSString *strUrlB64 = [NSString stringWithFormat:#"https://play.google.com/store/apps/details?id=com.ShiftSharerfree_new&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5TGlmdFNoYXJlcmZyZWVfbmV3Il0"];
NSData *dataUrl = [NSData dataFromBase64String:strUrlB64];
Then tried to send this to another url.
NSString *urlStr = [NSString stringWithFormat:#"http://server39.pnht.com/360ad/apps/ads/%#/android/136/ord0.910950258132325?json=1&package_url=%#",self.mStrPid, dataUrl];
It is giving me output: NSString *urlStr = [NSString stringWithFormat:#"http://server39.pbgs.com/360ads/apps/ad/%#/android/1360/ord0.910950252132325?json=1&package_url=%#",self.mStrPid, url];
It is not base 64. please guide how to get base 64 out of it.
I am using base 64 to encode and decode some text,
can u tell me if it works with URLs,
- (NSString *)base64Encode:(NSString *)plainText
{
NSData *plainTextData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainTextData base64EncodedString];
return base64String;
}
- (NSString *)base64Decode:(NSString *)base64String
{
NSData *plainTextData = [NSData dataFromBase64String:base64String];
NSString *plainText = [[NSString alloc] initWithData:plainTextData encoding:NSUTF8StringEncoding];
return plainText;
}

Cocoa error 256, When using initWithContentsOfURL:

i getting data from my site:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
contents have Cocoa error 256. where i wrong?
The issue is in Hebrew characters, you should html-escape them, also try request with English characters instead, to see if it works
- (void)yourMethod
{
NSString *name = #"שימרגוליס";
name = AFURLEncodedStringFromStringWithEncoding(name, NSUTF8StringEncoding);
NSString *website = [NSString stringWithFormat:#"http://www.ba-cafe.com/fbconnect.php?email=%#&name=%#&pass=SwHyK17!",#"email#mail.com",name];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
}
Where AFURLEncodedStringFromStringWithEncodingis a function from AFNetworking framework
Check the Console log for NSLog(#"%#", website);
You will see something like this:
http://www.mysite.com/fbconnect.php?email=thetrimmedname&name=emailaddress&pass=***
So do this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", [jsonObject objectForKey:#"email"], nameTrimmmed ];
instead of this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
This is because of the dot in the email address.
Look right here:
Error while trying to access Google translate with NSURL

how to capture data through web service (php) sql query select with condition from ios iphone sdk

I have the following php file:
<?php
$username="root";
$database="testdb";
mysql_connect(localhost,$username);
$user=$_GET["user"];
$password=$_GET["password"];
$query="SELECT documento FROM person WHERE user='".$user." and password ='".$password."'";
$result=mysql_query($query) or die (mysql_error("error "));
$num=mysql_numrows($result);
mysql_close();
$rows=array();
while($r=mysql_fetch_assoc($result)){
$rows[]=$r;
}
echo json_encode($rows);
?>
to retrieve the information achieved by the following functions but the example did not have the php input conditions and parameters was select * from person here the functions
based in http://www.youtube.com/watch?feature=endscreen&v=IQcLngIDf9k&NR=1
-(void) getData:(NSData *) data{
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
-(void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
now as the sql statement has input parameters is not as code and tried something like this A Simple PHP/MySQL Web Service for iOS but I can not accommodate what I need.
Pass the parameters in the string. Here is an example
-(void) start {
NSString *user = [[NSString alloc] initWithString:#"exampleuser"];
NSString *password = [[NSString alloc] initWithString:#"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://myserver.com/myphpfile.php?user=%#?password=%#", user, password];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
As it is password and user it normally is better to use POST instead of GET because then it is not part of the URL which is visible. With POST you can hide it if you have a https line. But I think this is not your main concern right now. The above should work.
I made some change in Hollfeder's code and it worked perfectly :)
-(void) start {
NSString *user = [[NSString alloc] initWithString:#"exampleuser"];
NSString *password = [[NSString alloc] initWithString:#"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://myserver.com/myphpfile.php?user=%#&password=%#", user, password];
NSString *urlstr_encoded = [urlstr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:urlstr_encoded];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}

Why does my NSURL object is equal to nil? My path is correct

NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude];
NSLog(#"urlString: %#", urlString);
NSURL *urlFromURLString = [NSURL URLWithString:urlString];
My log is : http://maps.google.com/maps/geo?q=53.872874,27.527790&output=csv&sensor=false&key=swizzlec hops
I can copy this url and paste to the browser and its ok, but urlFromURLString = nil. But, why?
I believe you need to use
stringByAddingPercentEscapesUsingEncoding:
on urlString before passing it to NSURL.
NSString *urlString = [[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude] stringByAddingPercentEscapesUsingEncoding::NSUTF8StringEncoding];
Try
NSString *urlString = [[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
I don't think you can have spaces in your URL
This is a better approach:
NSString* escapedUrl = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlFromURLString = [NSURL URLWithString:escapedUrl];

problem in converting NSString to NSData

I want to convert one NSString to NSData. on Using encoding am getting different value in NSData by displaying it using its description property.
I have
NSString *str=#"80369F4";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
on printing [data description]; am getting some different values...
how to extract the same string from the NSData.. pl help me out on this problem.
Create a new NSString:
NSString *newString = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
You can use NSString's initWithData:encoding: method:
NSString *str=#"80369F4";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *sameString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];