Ben Lefebvre
Ben Lefebvre

Reputation: 379

iPhone - Http Post : send a value to a web xml

i know there are a lot of questions about this but none seems to work for what I want to do. I want to change the value of a tag so let's say i have this file :

</Courbe>
<tempset>140</tempset>
</Courbe>

I want my http post request to change this value. How do I do this?

I have already tried something like that :

- (IBAction)changeTemp:(id)sender 
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL     URLWithString:@"http://207.134.145.16:50001/Courbe.xml"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];

NSString *xmlString = @"<tempset>137</tempset>";

[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

Is it something like this? Thanks for your help!

Upvotes: 0

Views: 817

Answers (1)

danh
danh

Reputation: 62686

Url encode the xmlString, then:

NSData *postData = [xmlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPBody:postData];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];

To send, use something like this:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {}];

Prior to iOS5, you can send asynchronously this way:

// make the request and an NSURLConnection with a delegate
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

// create a property to hold the response data, then implement the delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [responseData release];
    [textView setString:@"Unable to fetch data"];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}

Upvotes: 2

Related Questions