how to POST comment to wordpress site - iphone

Encountered a problem on posting comment back to a comment box.
post not showing up, there is no error or whatsoever to check.
EDIT: Correct Answer
-(void)postToBoard
{
NSString *post = #"author=Jos&email=mail#domain.com.au&url=&comment=score+99.&submit=Post+Comment&comment_post_ID=1&comment_post_ID=1&comment_parent=0";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSString *URLString = #"http://site.com.au/?p=1";
NSURL *postURL = [NSURL URLWithString:URLString];
[request setURL:postURL];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
//[request setValue:#"wp-comments-post.php" forHTTPHeaderField:#"HTTP/1.1"];
[request setHTTPBody:postData];
//self.receivedData = [NSMutableData data];
self.postConnection = [NSURLConnection connectionWithRequest:request delegate:self];
}

This line looks wrong:
[request setValue:#"wp-comments-post.php" forHTTPHeaderField:#"Content-Type"];
It should be:
[request addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
I'm not sure what you're supposed to do with "wp-comments-post.php", should that perhaps be in the URL instead?

Related

Login credential issue using NSURL Methods

I have an webservices url which appends with password. When I run the application by giving correct password, the application is running and at the same time when I run the app with wrong credentials. the app is running, I didn't have an idea to get rid out of the issue. My code is here:
NSString *post =[[NSString alloc] initWithFormat:#"password=%#",[password text]];
NSLog(#"PostData: %#",post);
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"http://myexample.com/Accountservice/Secure/ValidateAccess?password=abcd&type=1"]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:post] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request1 delegate:self startImmediately:YES];
if(!connection){
NSLog(#"connection failed");
}
else{
NSLog(#"connection succeeded");
}
If I gave 'abcd' as password connection is successfully is displaying. If I gave wrong password connection successfully is displaying. How can I solve the issue? If I gave wrong password need to display an alert.
This control structure here is the problem:
if(!connection){
NSLog(#"connection failed");
}
else {
NSLog(#"connection succeeded");
}
This doesn't check for a bad connection, this checks whether or not the connection object is equal to 0 (nil). Obviously, having initialized it on the line above, the else statement will always log, giving you the false impression that your connection had succeeded. You should become the connection's delegate, and respond to the appropriate delegate methods instead.
The issue may be with the url you are using
Fire the url in the browser and see what it returnes
try add the url params with single quotes accesscode='abcd'
It is not a good practice to pass the credentials via this way.
You could use POST method with some valid encryption to prevent the security issues that can occur
SOLUTION
Try this
NSString *urlString=#"http://myexample.com/Accountservice/Secure/ValidateAccess?";
self.responseData = [NSMutableData data];
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",userNameTextField.text,passwordTextField.text];
NSData *postData=[post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection0= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection0 start];

Issuing a post request iPhone->Server from xcode

I've done many searches but none of the code fragments I find seem to work for me. I have the following fragment in xcode for running within my iPhone app:
NSURL *url = [NSURL URLWithString:#"http://blah.blah.com/rec.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSData *body = [#"data=crap" dataUsingEncoding:NSUTF8StringEncoding];
NSString *bodyLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:bodyLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:body];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
The rec.php is called fine but the post data does not make it through. The entry $_POST['data'] is empty. Could anyone let me know what I'm doing wrong?

i want to send request by POST on iPhone,but the body can't be received by server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iPhone SDK Xcode 4.2 iOS 5- How do i send a json to url ? (POST not GET) [Resolved]
NSDictionary *requestDic=[NSDictionary dictionaryWithObjects:values forKeys:keys];
NSString *jsonreq=[requestDic JSONRepresentation];
NSURL *requrl=[NSURL URLWithString:[NSString stringWithFormat:#"%#index.php?r=%#&te=%#&noCheck=1&checkInstall=start",[Info serverUrl],[dictParameters objectForKey:#"r"],[dictParameters objectForKey:#"te"]]];
requrl = [NSURL URLWithString:[[requrl absoluteString] stringByAppendingString:subUrl]];
NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:requrl];
[req setHTTPMethod:#"POST"];
[req setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[req setValue:#"application/json-x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *requestData=[NSData dataWithBytes:[jsonreq UTF8String] length:[jsonreq length]];
[req setHTTPBody:requestData];
[browser loadRequest:req]
Dilshand's answer is ok , but you can use ASIFormDataRequest for ease. like follow
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:mobileApplicationId forKey:#"mobileApplicationId"];
[request setPostValue:mobileApplicationVersion forKey:#"mobileApplicationVersion"];
[request setPostValue:userID forKey:#"userId"];
[request setPostValue:pass forKey:#"password"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
What you want to post server give complete detail and what error you are getting
try following i have done using this to send data to server and it works fine for me hope this helps
NSString *post =[[NSString alloc] initWithFormat:#"ProviderNPI=%#&PatientID=%#&FileURL=%#&FileTYPE=%#&DataSynID=%#& AppointmentListingsID=%#",providerNPI,patientID,FILEURL,FILETYPE,dataSynID,appointmentListingID];
NSURL *url=[NSURL URLWithString:#"http://myserver.com/Sync.php?"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",data);
NSURL *url = [NSURL URLWithString:#"http://www.invoicera.com/app/api/check_json_api.php?token=7B92C122473A3D6F54E60D20AC5526D0"];
NSString *jsonRequest = [NSString stringWithFormat:#"&json_data=%#",[[NSString stringWithFormat:#"{\"listInvoice\":{\"client_id\":\"\",\"date_from\":\"\",\"date_to\":\"\",\"invoice_number\":\"\",\"invoice_record_status\":\"\",\"invoice_status\":\"\",\"page\":\"1\",\"per_page_record\":\"20\"}}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",jsonRequest);
NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSLog(#"%#",json_data);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: json_data];
NSLog(#"%#",json_data);
// [request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
//[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [json_data length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES]];
// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
// Successful connection.
if (nsUrlConnection) {
// [self initSpinner];
// [self spinBegin];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData=data;
[data release];
}
// Unsuccessful connection.
else {
}
// Clean up
[url release];
[request release];
// Close keypad.

how to write data on server?

i am making a registration page were user can register?
this is my code..............
-(IBAction)btn_log:(id)sender
{
//NSString* username = nameInput.text;
//NSString* pass = passInput.text;
NSString* firstname = nameInput.text;
NSString* lastname = passInput.text;
NSString* bname = lastInput.text;
NSString *post =
[[NSString alloc] initWithFormat:#"fname=%#&lname=%#&email=%#",firstname,lastname,bname];
//NSString *post = [[NSString alloc]initWithFormat:#"fname=firstname&lname=lastname&email=bname];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) NSLog(#"Connection Successful");
}
But it's not working so please can anyone suggest me where i am making mistake.
try in this way may be it will help you
Nsurl *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php"]]];
nsstring *body = [nsstring stringwithformat:#"%#,%#",nameinput,paasinput];
nsmutableurlrequest *request = [nsmutablerequest alloc]initwithurl:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request sethttpbody:[body dataysubgebcidubg:asciiiencoding allowlossyconversion : true]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) NSLog(#"Connection Successful");
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Replace above line of your code with following line. Probably it will work.
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

How to append querystring in url using objective c?

NSString *post = #"&username=adam&password=test";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx?uname=adam&pword=test"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *response = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSLog(response);
This is my url http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx, now I need to append the username and password dynamically.
How to append querystring in url? Please help me.
You can do it this way:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx?uname=%#&pword=%#",name, password]]];