【问题标题】:How to get the session id from NSURLConnection - IOS SDK如何从 NSURLConnection 获取会话 ID - IOS SDK
【发布时间】:2012-09-28 06:48:22
【问题描述】:

如何从 NSURLConnection 获取会话 ID?

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

谢谢。

【问题讨论】:

  • 会话ID是什么意思?你试过什么?
  • 使用网络服务,我们可以创建一些会话,并为登录服务提供一些会话ID。如果我使用 NSURLConnection 连接一些 API,则创建了一些会话。 IOS能不能抓到session id?

标签: iphone ios ipad session nsurlconnection


【解决方案1】:

如果您的请求作为会话 id 获得反馈,请使用 NSURLConnection 的委托来获取会话 id:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:
    (NSURLResponse *)response
{
    // Discard all previously received data.
    [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data
{
    // Append the new data to the receivedData.
[receivedData appendData:data];     
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Connection succeeded in downloading the request.
    NSLog( @"Succeeded! Received %d bytes of data", [receivedData length] );

    // Convert received data into string.
    receivedString = [[NSString alloc] initWithData:receivedData 
        encoding:NSASCIIStringEncoding];
    //receivedString will have session id if request is appropriate
    NSLog( @"From connectionDidFinishLoading: %@", receivedString );

    // release the connection, and the data object
    [conn release];
    [receivedData release];
}

您必须在 .h 文件中设置 Protocol NSURLConnectionDataDelegate,NSURLConnectionDelegate

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多