-3

如何从 NSURLConnection 获取会话 ID?

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

谢谢你。

4

1 回答 1

0

如果您的请求作为会话 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

于 2012-09-28T06:56:44.260 回答