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

我想在Objetive C中调用返回xml数据的Web服务,我必须在头文件中传递一些信息到服务器,
就像在 javascript中,我们可以使用jquery来做,

x.setRequestHeader( ‘键’,‘值’);

其中x是xmlHttpRequest对象.
我们如何在NSConnection类中传递标题数据,
我用google但是没有找到好的解决方案.
请帮帮我.

解决方法

您可以使用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];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...