0

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

有一些视觉 - http://www.test.com/event (这是一个例子)

-(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];
    }
}

例如,当我单击此链接时,我有一个带有用户名和密码屏幕的警报..当我输入我可以访问的信息时..此代码用于连接我的网络服务,我该如何管理?

4

1 回答 1

1

您需要将身份验证标头添加到您的theRequest对象。

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"];
于 2012-12-03T23:07:19.463 回答