如何在Objective-C中设置Http头字段? [英] How to set Http header Fields in Objective-C?

查看:135
本文介绍了如何在Objective-C中设置Http头字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to call web service in Objetive C which returning xml data and I have to pass some information in header to the server, just like in javascript we can do it using jquery,

x.setRequestHeader('key','value');

x.setRequestHeader('key','value');

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

where x is the xmlHttpRequest Object. How we can pass the header data in NSConnection class, I used google but havent find out good solution. please help me.

推荐答案

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

You can pass the information in header using NSMutableURLRequest class and then call the NSURLConnection class(it will call the connection 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];

这篇关于如何在Objective-C中设置Http头字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆