0

これについてはたくさんの質問があることは知っていますが、私がやりたいことにはどれもうまくいかないようです。タグの値を変更したいので、このファイルがあるとしましょう:

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

httppostリクエストでこの値を変更したい。どうすればよいですか?

私はすでにそのようなことを試しました:

- (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];
}

こんな感じですか?ご協力いただきありがとうございます!

4

1 に答える 1

2

xmlStringをUrlエンコードしてから、次のようにします。

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"];

送信するには、次のようなものを使用します。

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

iOS5より前は、次のように非同期で送信できます。

// 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];
}
于 2012-08-06T18:35:39.513 に答える