Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

ASIHTTPRequest POST Response

AridifAridif Posts: 12Registered Users
edited December 2009 in iOS SDK Development
Hi, I'm having trouble getting a response (or even sending data) with my application. I'm a real noob, so please bear with me ;) I'm using the ASIHTTPRequest. The setup is so that when i press a button, the response will show up in a label.
- (IBAction)pReSsEd:(id)sender {
	NSURL *url = [NSURL URLWithString:@"http://www.some-site.org"]; 
	
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
	[request setPostValue:@"Ben" forKey:@"ctl00_cphContent_txtFirstname"];
	[request setPostValue:@"Copsey" forKey:@"ctl00_cphContent_txtLastname"];
	
	NSError *error = [request error];
	if (!error) {
		NSString *response = [request responseString];
		sTaTuS.text = response;
	}
	else {
		sTaTuS.text = @"Phail!";;
	}
}

So I have these questions;

1. The form on the site has method = post (according to the source code). the different boxes, first name and last name, has both a "name" and an "id". Which one whould i use after "forKey"?

2. Am I using the error correct?

3. Will the response thing work, as I have written it now?

Huge thanks if you answer any of these questions :D

/Johan
Post edited by Aridif on
· ·

Replies

  • jsdjsd Posts: 900Registered Users
    edited December 2009
    You never started the request. Add a [request start]; after the last setPostValue.
    · ·
  • AridifAridif Posts: 12Registered Users
    edited December 2009
    jsd wrote: »
    You never started the request. Add a [request start]; after the last setPostValue.

    Thanks for the reply! I've updated the code to look lite this;
    - (IBAction)pReSsEd:(id)sender {
    	NSURL *url = [NSURL URLWithString:@"http://www.birthday.se/sok"]; 
    	
    	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    	[request setRequestMethod:@"GET"];
    	[request setPostValue:@"Johan" forKey:@"ctl00$cphContent$txtFirstname"];
    	[request setPostValue:@"Svensson" forKey:@"ctl00$cphContent$txtLastname"];
    	[request start];
    	
    	NSError *error = [request error];
    	if (!error) {
    		NSString *response = [request responseString];
    		sTaTuS.text	= response;
    	}
    	else {
    		sTaTuS.text	= @"Phail!";;
    	}
    }
    

    But it still gives me the Phail! message...

    (also updated the code with the real url)

    Got any ideas?
    · ·
  • jsdjsd Posts: 900Registered Users
    edited December 2009
    You need to print the actual error message. Instead of Phail, put [error localizedDescription]
    · ·
  • AridifAridif Posts: 12Registered Users
    edited December 2009
    jsd wrote: »
    You need to print the actual error message. Instead of Phail, put [error localizedDescription]

    Thanks, now i get the message; "Unable to obtain information on proxy servers needed..."

    Changed the line to:
    sTaTuS.text = [error localizedDescription];
    

    Appreciate your help ;)
    · ·
  • jsdjsd Posts: 900Registered Users
    edited December 2009
    Weird, that sounds like some sort of network configuration problem. I ran the code just now and I got "<html><head><title>Error</title></head><body>Incorrect function.
    </body></html>"
    · ·
  • AridifAridif Posts: 12Registered Users
    edited December 2009
    jsd wrote: »
    Weird, that sounds like some sort of network configuration problem. I ran the code just now and I got "<html><head><title>Error</title></head><body>Incorrect function.
    </body></html>"

    Strange, but do you know what your error means?
    · ·
  • jsdjsd Posts: 900Registered Users
    edited December 2009
    Looks like the web server doesn't like whatever you're trying to feed it. Probably some missing hidden parameter or something like that.
    · ·
  • AridifAridif Posts: 12Registered Users
    edited December 2009
    Ok, I screwed up a bit, the code should be without the "[request setRequestMethod:@"GET"];" part. Forgot to remove it when I tested a few things...

    Here is the real deal, would you mind to run it again? :)
    - (IBAction)pReSsEd:(id)sender {
        NSURL *url = [NSURL URLWithString:@"http://www.birthday.se/sok"];
        
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:@"Johan" forKey:@"ctl00$cphContent$txtFirstname"];
        [request setPostValue:@"Svensson" forKey:@"ctl00$cphContent$txtLastname"];
        [request start];
        
        NSError *error = [request error];
        if (!error) {
            NSString *response = [request responseString];
            sTaTuS.text    = response;
        }
        else {
            sTaTuS.text    = [error localizedDescription];
        }
    }
    
    · ·
  • jsdjsd Posts: 900Registered Users
    edited December 2009
    Same error.
    · ·
  • AridifAridif Posts: 12Registered Users
    edited December 2009
    Hi, tried on my netbook instead, and got the same error as you. Then i added a / in the end of the url, and got a different error message! Making progress ;)

    EDIT: It's not a error! I's actually a real connection to the server! Thank you for the help, you are awesome! :D

    EDIT2: Ok, I now have a response from the server, but the response seems to be the source code of the side before I have made the search, and not after. Do you have any ideas?
    · ·
  • jsdjsd Posts: 900Registered Users
    edited December 2009
    There's a hidden form variable called __VIEWSTATE that probably has something to do with it. Your best bet would be to trace what a real web browser posts to that form, use something like the Charles Debugging Proxy.
    · ·
  • AridifAridif Posts: 12Registered Users
    edited December 2009
    Not sure if i'm following you, but here is the url when I have searched for "Johan Svensson":
    http://www.birthday.se/sok/?f=johan&l=svensson
    

    it ads first a "?" and then f (for fistname) = johan & l (last) = svensson.

    Don't really know what i whould do with it...
    · ·
  • jsdjsd Posts: 900Registered Users
    edited December 2009
    I can't read swedish so I don't know what the point of that site is, but I'm guessing you want to simulate entering data into that form and clicking the submit button. If so, you are on the right track, but you need to make your form post contain all the variables that a real web browser would submit. If you look at the source of that page you'll see some hidden form fields in the form. If you don't pass those, I'm guessing that's what makes the server generate the Invalid Operation message. So you're gonna need to do some detective work to figure out exactly what's going on when you hit submit in your web browser, and try to mimic it using ASIFormDataRequest parameters. Good luck!
    · ·
  • RyanWRyanW Posts: 94Registered Users
    edited December 2009
    Aridif wrote: »
    Not sure if i'm following you, but here is the url when I have searched for "Johan Svensson":
    http://www.birthday.se/sok/?f=johan&l=svensson
    

    it ads first a "?" and then f (for fistname) = johan & l (last) = svensson.

    Don't really know what i whould do with it...

    Is the website a PHP written website? If so you would use
    $var = $_GET["f"];
    <div align="center">Ryan Walters - Web & Software Developer<br />
    <a href="http://tenimizer.com" target="_blank">View my portfolio</a> - <a href="http://itunes.apple.com/us/artist/ryan-walters/id3185435678" target="_blank">My iPhone Applications</a><
    · ·
Sign In or Register to comment.