首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Encryption iOS Xcode

Encryption iOS Xcode
EN

Stack Overflow用户
提问于 2012-12-04 06:59:11
回答 1查看 829关注 0票数 0

我对私有的webservice有一个问题。我的意思是为了访问这个网络服务,我需要用户名..我怎样才能进入我的webservice .Do ?你有什么建议吗?

要有可视化的东西-- http://www.test.com/event (这是一个例子)

代码语言:javascript
复制
-(void)startConnection
{
    NSString *urlString = GET_EVENT_URL_STRING;

    NSURL *url = [NSURL URLWithString:urlString];

    if (!url)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL from string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

        theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];

    // Set the HTTP method of the request to POST
    [theRequest setHTTPMethod:@"POST"];


    if (!theRequest)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL request from string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (!theConnection)
    {
        NSString *reason = [NSString stringWithFormat:@"URL connection failed for string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

    if (theConnection)
    {
        myData = [[NSMutableData alloc]init];
    }
}

例如,当我单击此链接时,我会看到一个带有用户名和密码屏幕的警报,并输入我可以访问的信息。这段代码用于连接我的how服务,我该如何管理它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-04 07:07:19

您需要向theRequest对象添加身份验证头。

代码语言:javascript
复制
NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13693291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档