NSURLconnection委托不会被调用

时间:2011-09-17 08:14:32

标签: iphone objective-c ios ipad nsurlconnection

我的NSURLConnection代表没有被调用。我正在尝试这样做,而不是使用此[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[[NSURL URLWithString: urlAddress] host]];这不适用于appstore。

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
myError.hidden = FALSE;
NSHTTPURLResponse   * response;
NSError             * error;
NSMutableURLRequest * request;
NSString            * params;
NSString *urlAddress = [NSString stringWithFormat:@"%@/?action=request&api=json&module=ManagementModule&function=startSession&instance=0",[ConnectServer returnserverip]];
NSLog(@"UPX %@",[ConnectServer returnserverip]);
NSLog(@"IP %@",[ConnectServer returnclientip]);
if([defaults boolForKey:@"enablePincode"]){
    NSString *account = [defaults stringForKey:@"myAccount"];
    NSString *username =[defaults stringForKey:@"myUsername"];
    NSString *password = [defaults stringForKey:@"myPassword"];
    NSString *clientip = [ConnectServer returnclientip];
    NSString *clientname = [ConnectServer returnclientname];
    params = [[[NSString alloc] initWithFormat:@"params=&auth[password]=%@&auth[mode]=%@&auth[account]=%@&auth[user]=%@&auth[rights]=%@&auth[user_ip]=%@&auth[client_name]=%@",password,@"password",account,username,@"user",clientip,clientname] autorelease];

request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlAddress] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60] autorelease];
NSData *myRequestData = [params dataUsingEncoding:NSUTF8StringEncoding];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[[NSURL URLWithString: urlAddress] host]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:myRequestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
request.URL = [NSURL URLWithString:urlAddress];
error       = nil;
response    = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
>>>>>>NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self ];
>>>>>>[connection start];
NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]);
NSLog(@"Parameters: %@", params);
NSLog(@"Actual sended parameters to the server: %@", myRequestData);
NSString *Sresponse;

1 个答案:

答案 0 :(得分:0)

1-确保你让代表自己,我认为你这样做。 2 - 还要确保你没有使用像NSOperationINvocation这样的其他线程。 3-执行上述代码时确保可以尝试

[self performselectoratmainthread:@selecor(function :) with object:Object];

NSString *ips = [NSString stringWithFormat:@"http://10.0.0.0:2552/NewsAccessService.asmx"];
    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             "<SOAP-ENV:Envelope \n"
                             "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
                             "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 
                             "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
                             "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
                             "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
                             "<SOAP-ENV:Body> \n"
                             "<GetAlbumPhotos xmlns=\"http://tempuri.org/\"/> \n"
                             "</SOAP-ENV:Body> \n"
                             "</SOAP-ENV:Envelope>"];
    NSURL *url = [NSURL URLWithString:ips];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];                         
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];              
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/GetAlbumPhotos" forHTTPHeaderField:@"Soapaction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];     
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
    if(theConnection) {
        webData = [[NSMutableData data] retain];
        AlbumPhotos = [[NSMutableArray alloc] init];
        x = 1;
        return x;
    }

处理代表这样的

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //NSLog(@"Connection failed: %@", [error description]);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
}