0

我知道对此有很多问题,但似乎没有一个适合我想做的事情。我想更改标签的值,所以假设我有这个文件:

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

我希望我的 http post 请求更改此值。我该怎么做呢?

我已经尝试过这样的事情:

- (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

Url 对 xmlString 进行编码,然后:

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 回答