views:

1757

answers:

3

Hi all,

I need to call a url from my iphone when i tap on a button.

The url is taking a value from the UITextField as argument, also it is using a POST method to invoke the webservice. how do i use the URL in my iphone. I had done the same with GET method. but for that i created a url string with the value from the textfield. it cant be use for the POST one. so what shoud i do for that. is there any sample code for this.. Also the web service is a ".asmx" type. This is what i had done.....

-(IBAction)loginButtonPressed:(id)sender{
NSString *post =[NSString stringWithFormat:@"name=myUserName&pass=myPassword"];
NSLog(post);  

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://192.168.50.194:9989/webservice1.asmx?op=LoginAuthentication"]];
[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);

}

The error i am getting is

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;soap:Body&gt;&lt;soap:Fault&gt;&lt;soap:Code&gt;&lt;soap:Value&gt;soap:Receiver&lt;/soap:Value&gt;&lt;/soap:Code&gt;&lt;soap:Reason&gt;&lt;soap:Text xml:lang="en">Server was unable to process request. ---&gt; Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

Please Help me.!!!

Thanks, Shibin

+3  A: 

You can set the HTTP method to use POST instead of the default GET using something like this:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:@"POST"];
Hutaffe
Thanks, but how do i pass the parameters...?
Shibin Moideen
The link provided in the comment by "The MYYN" above does exactly what you need. Create a string containing your parameters just like you would in a normal url, create an NSData object from that and provide this to your request object using setHTTPBody:
Hutaffe
i updated the question... can u please help me...?
Shibin Moideen
Well... the code looks quite right to me. I guess it's more a problem of what you give to your webservice?
Hutaffe
No, the webservice works fine in a windows machine. and the user data is fetched properly. i am not sure about whats happening..
Shibin Moideen
I don't know that much about WebSerivces but I was just googling a bit and found the following article. Seems like you have to wrap your parameters into a soap request (you get a soap error, so i assume you need that)... http://www.devx.com/wireless/Article/43209/0/page/3
Hutaffe
You might also want to look at that question here: http://stackoverflow.com/questions/1667787/access-a-web-service-in-iphone and here
Hutaffe
Thaks Hutaffe... i am not sure about what to add when i am using a .net web service. any idea.???
Shibin Moideen
WebService is WebService ;) Your iPhone does not care if your backand is .net or perl. The article which link I posted before describes in detail how to integrate an iPhone App with a .net WebService with SOAP, even using HTTP POST. Heres the link to the start of the article: http://www.devx.com/wireless/Article/43209/0/page/1
Hutaffe
Thanks friend. i tried this also http://grabalife.com/2009/10/19/using-net-web-services-and-dataset-objects-in-your-iphone-app/ seems good. thanks for you valuable suggestions.
Shibin Moideen
To pass the parameters with request see http://urenjoy.blogspot.com/2009/12/pass-parameter-web-service-image-upload.html
Brij
+1  A: 

Hi all, i got it working thanks for your support.. here is the working code...

-(IBAction)loginButtonPressed:(id)sender{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(loginID.text);
NSLog(loginPassword.text);
NSString *soapMsg = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;&lt;soap:Body&gt;&lt;IphoneAuthenticateduser xmlns=\"http://tempuri.org/\"&gt;&lt;UserName&gt;%@&lt;/UserName&gt;&lt;Passwordtext&gt;%@&lt;/Passwordtext&gt;&lt;/IphoneAuthenticateduser&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;",loginID.text,loginPassword.text];
NSLog(soapMsg);
NSURL *url = [NSURL URLWithString:@"http://192.168.50.194:9989/Iphoneauthentication.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/IphoneAuthenticateduser" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

if(!response){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error"
                                                    message:@"Failed to Connect to the Internet"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
else{
    xmlResult=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:urlData];
    XMLParser *parser = [[XMLParser alloc] initXMLParser];
    [xmlParser setDelegate:parser];
    sucess = [xmlParser parse];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if(sucess){
        NSLog(appDelegate.message);
        if(appDelegate.message != NULL){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed"
                                                            message:appDelegate.message
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
            appDelegate.message = NULL;
        }
        else{
            UserViewController *userController = [[UserViewController alloc] initWithNibName:@"UserViewController"
                                                                                  bundle:nil];
            [self presentModalViewController:userController animated:YES];
            [userController release];
        }
    }
    else{
        NSLog(@"Parsing Failed");
    }
}
[pool release];

}

Shibin Moideen
A: 

Dear Shibin and Hutaffe,

Would you mind to explain the working code again as I am a green of the iPhone application, I do not understand what are the application. For the variable *soga, is necessary for all the application to wirte the such long link or it is only for application.

In my iPhone application, I also need to pass the parameters to the url by clicking a button and it must use the POST. It may occur the 302 and 303 error. Please help me.

Thank you.

Questions