我想在返回 xml 数据的 Objetive C 中调用 Web 服务,我必须将 header 中的一些信息传递给服务器, 就像在 javascript 中一样,我们可以使用 jquery 来完成,
x.setRequestHeader('key','value');
其中 x 是 xmlHttpRequest 对象。 我们如何在 NSConnection 类中传递 header 数据, 我用谷歌但还没有找到好的解决方案。 请帮我。
最佳答案
您可以使用 NSMutableURLRequest 类在 header 中传递信息,然后调用 NSURLConnection 类(它将调用连接委托(delegate))。
看下面的代码,
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];
关于ios - 如何在 Objective-C 中设置 Http header 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116192/