如何在Objective-C中设置Http头字段?

我想调用在返回XML数据的对象C的Web服务,我必须传递一些信息在头到服务器,就像在JavaScript中,我们可以做到这一点使用jQuery,

x.setRequestHeader( '键', '值');

其中x是xmlHttpRequest对象。 我们如何在NSConnection类中传递标题数据,我使用谷歌,但没有find好的解决scheme。 请帮帮我。

您可以使用NSMutableURLRequest类在头中传递信息,然后调用NSURLConnection类(它将调用连接委托)。

看下面的代码,

NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myServerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; //do post request for parameter passing [theRequest setHTTPMethod:@"POST"]; //set the content type to JSON [theRequest setValue:@"xml" forHTTPHeaderField:@"Content-Type"]; //passing key as a http header request [theRequest addValue:@"value1" forHTTPHeaderField:@"key1"]; //passing key as a http header request [theRequest addValue:@"value2" forHTTPHeaderField:@"key2"]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if( theConnection ) { webData = [[NSMutableData data] retain]; } else { NSLog(@"theConnection is NULL"); } [theConnection release];