FabioN1975
FabioN1975

Reputation: 45

XCode GET POST WebServer

I'm beginner from Xcode. I have to do an application that get and send data from embedded webserver that I designed in a microcontroller MICROCHIP. Now I'm able to get information from webserver ( In my webserver I have a /status.xml file where I have all the dynamic variables)

Now I am not able to click a button. The HTML code to click a button is

onmousedown="newAJAXCommand('buttons.cgi?btn=1')"

In my webserver I have a file buttons.cgi.

My target is designed a button in Xcode that he does this action

I tried to use NSMutableURLRequest class and SetHTTPMethod:@"GET" or @"POST" but this code doesn't work

    NSString *post = @"btn=1";
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES ];
    NSString *Get_lenght = [NSString stringWithFormat:@"%d",[postData length]];
    NSURL *serviceURL = [NSURL URLWithString:@"http://mywebserver.com/buttons.cgi?"];
    NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceURL];
    [serviceRequest setValue:Get_lenght forHTTPHeaderField:@"Content-type"];
    [serviceRequest setHTTPMethod:@"POST"];
    [serviceRequest setHTTPBody:postData];

Upvotes: 0

Views: 397

Answers (1)

Raptor
Raptor

Reputation: 54212

There are multiple errors in the codes:

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES ];

Why don't you use UTF-8 ?

NSString *Get_lenght = [NSString stringWithFormat:@"%d",[postData length]];
[serviceRequest setValue:Get_lenght forHTTPHeaderField:@"Content-type"];

You can't set Content-Type as an integer.

Last, you didn't send out the NSMutableURLRequest. To send out the request, use :

NSString *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&response error:&error];

where response will contain the response data, if you need it; and the error will contain error during request & response, if there is any error.

Upvotes: 1

Related Questions