ASIHTTPRequest setPostValue iPhone - 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];

Related

cURL web services from iphone

I need to fetch data from curl web services from iOS, the client provided me a header name, header value and a base url, I dont know what to do with them, the url isn't giving me anything opening in a browser. They mentioned data is JSON encoded.
Please link me to some library or tutorial on how to call them.
You can use ASIHTTPRequest for cURL as well.
what I have used is:
NSURL *url = [NSURL URLWithString:url];
__weak ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
request.shouldPresentCredentialsBeforeChallenge = YES;
[request appendPostData:[JSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request setUsername:username];
[request setPassword:password];
[request setRequestMethod:#"PUT"];
request.timeOutSeconds = 30;
request.validatesSecureCertificate = NO;
[request startAsynchronous];
solved it, had to set NSURLRequest http method and header like
[request setHTTPMethod:#"GET"];
[request setValue:#"HEADER_VALUE" forHTTPHeaderField:#"HEADE_RNAME_"];

Creating XML requests and sending them to server using ASIHttpRequest

I am trying to generate XML request to be sent to server over Http Post. I am aware of ASIHttpRequest library and want to use it.
I have two questions regarding this:
1) How to generate XML request? (Which is best library to use? GData/libXML/KissXML? or else?)
2) How to send it to server using ASIHttpRequest?
Please help.
Thanks in advance.
Achieved it this way:
NSString* newStr = [[NSString alloc] initWithData:postBody
encoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlstr];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"r" forKey:newStr];
request.delegate = self;
[request startSynchronous];

Objective-C and Preemptive Authentication

I am trying to get data from WebService for my Ipad App.
To do that, I am using a NSURLConnection with NSMutableURLRequest.
Web Services are published on Apache-Coyote 1.1 with Preemptive Basic Authentication, but I don't know how to send my credentials from Objective-C.
Anybody knows how can I set my user/password in Objective-C to log my clients with the Apache premptive authentication system?
Thank you.
EDIT:
In order to set the creds yourself, you'll need to be able to base64 encode the username and password and set the appropriate header. Matt Gallagher, from Cocoa With Love, has a great post on how to add a category to NSData to easily do this.
NSString* username = #"username";
NSString* password = #"password";
NSString* encodedUsername = [[username dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString* encodedPassword = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSURL* url = [NSURL URLWithString:#"http://yourUrl.com/"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
NSString* headerValue = [NSString stringWithFormat:#"Basic %#:%#", encodedUsername, encodedPassowrd];
[request addValue:#"Authorization" forHTTPHeaderField:headerValue];
[NSURLConnection connectionWithRequest:request delegate:self];
As with all use of credentials, please make sure you are doing this all over HTTPS because these credentials are essentially being passed in clear text.
Consider using the ASIHTTPRequest framework, which makes preemptive Basic authentication requests simple:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; /** preemptively present credentials **/
[request setUsername:#"username"];
[request setPassword:#"password"];
[request startSynchronous];
NSError *error = [request error];
if (!error)
NSString *response = [request responseString];
And, yes, you definitely want to do Basic authentication over SSL (i.e. via some https://... URL).

Post request from iPhone using ASIFormDataRequest not working

Newbie to Rails/iOS here. I have a rails blog application. I'm trying to upload a post from iOS using an HTTP POST method with ASIFormDataRequest. Here's the code that get's called:
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setFile:#"star.png" forKey:#"image"];
[request startSynchronous];
NSString *response = [request responseString];
NSLog(#"response:: %#", response);
When I run this code, nothing happens. My server does not get contacted. I get response:: (null). Any idea why?
EDIT I found out that star.png needed to have its full file address. Now the POST works fine, but neither the name or image get saved into the db. A new post with empty name and image gets created. Any idea why?
why you are using localhost here?
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
use your web server ip instead of localhost
NSURL *url=[[NSURL alloc] initWithString:#"http://yourservername:3000/posts"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setPostValue:imageData forKey:#"image"];
[request startSynchronous];
For the "imagePath" is the path to your image. The full path.

uiwebview session problem

NSURL *url = [NSURL URLWithString:#"http://ip/login"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
NSString *body = [NSString stringWithFormat: #"name=%#&pass=%#", #"phpxiaoxin", #"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[request setHTTPMethod: #"POST"];
//Load the request in the UIWebView.
[webView loadRequest:request];
url = [NSURL URLWithString:#"http://ip/hotel/hotelReservationList.action"];
request = [[NSMutableURLRequest alloc]initWithURL: url];
[webView loadRequest:request];
hi all i have a problem with uiwebview cause of session;
now first i wan to use a postmethod to login the system
then i need to control to redirect to the action of "hotelreservation.action"
but the second request is not extend the first session ,so the result is the web system redirect to login page
what i should do with uiwebview, i know i can redirect is in web but i do need to control by webview.
so anybody can help me ? thanks a lot
You should execute the second request in the UIWebViewDelegate's webViewDidFinishLoad: method.