使用密码从URL获取数据

时间:2011-11-05 14:52:25

标签: ios ios5

我想从此网址获取数据:http://webservices.ns.nl/ns-api-stations。此URL需要登录。如何将此登录信息放入我的代码中?这段代码就是我现在使用的。

- (void)fetchStationData {     //获取数据的方法

//First lets check wheater there is an internet connection and if the host is reachable
if(internetActive) {

    //Internet is active

    //Init the parser
    parser = [[RSSParser alloc] init];

    //Set he parser context
    parser.context = context;

    //The array to het the data from 
    NSURL *url = [NSURL URLWithString:@"http://webservices.ns.nl/ns-api-stations"];

    //The XML elements to fetch
    NSArray *elements = [[NSArray alloc] initWithObjects:@"naam",nil];

    //The actual fetchin
    [parser fetchStationItemsForUrl:url forElements:elements];

    //Save the context ?
    [context save:nil];

    //Clean up
    [elements release]; 

}

2 个答案:

答案 0 :(得分:0)

您尝试访问并获取某些数据的服务器需要某种身份验证才能允许它。因此,在尝试发出get请求之前,您需要执行身份验证。因此,您需要知道他们使用哪种身份验证方法(例如Digest Auth),还需要用户名和密码..

您还需要使用HTTPRequest库。我建议您使用ASIHTTPRequest,它非常简单。您还可以在从服务器发出请求之前使用asihtttprequest库执行身份验证。

答案 1 :(得分:0)

您关联的网站受basic auth保护。传递身份验证信息的一种更简单的方法是将凭据附加到URL:

http://user:password@webservices.ns.nl/ns-api-stations

请注意,网站不受SSL保护,因此所有身份验证信息都将以明文形式传递。