didFailWithError:错误域= kCFErrorDomainCFNetwork代码= 303"当GET请求时无法完成操作

时间:2014-12-12 13:28:58

标签: ios objective-c iphone get-request

我在拨打网络服务时遇到以下错误。

  

didFailWithError:错误域= kCFErrorDomainCFNetwork代码= 303"   操作无法完成。 (kCFErrorDomainCFNetwork错误303。)"   的UserInfo = 0x16e505b0   {NSErrorFailingURLKey = http://trac.app.com/api/v1/engine/chat/,   NSErrorFailingURLStringKey = http://trac.app.com/api/v1/engine/chat/}
  我的源代码如下:
  webservice.m

-(void)callChatWebservice:(NSString *)parameter
{
    NSString *requestString =[NSString stringWithFormat:@"http://trac.app.com/api/v1/engine/chat/"];

    //NSString *jsonString = [NSString stringWithFormat:@"{\"q\":\"%@\",\"uid\":\"%@\"}",[parameter valueForKey:@"q"],[parameter valueForKey:@"uid"]];

    //NSString *jsonString = [NSString stringWithFormat:@"{\"uid\":\"%@\"}",[parameter valueForKey:@"uid"]];
    NSString *jsonString = [NSString stringWithFormat:@"{\"uid\":\"%@\"}",parameter ];
    //NSString *jsonString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"email\":\"%@\",\"password1\":\"%@\",\"password2\":\"%@\",\"location\":[%@,%@]}",[parameter valueForKey:@"username"],[parameter valueForKey:@"email"],[parameter valueForKey:@"password1"],[parameter valueForKey:@"password2"],[parameter valueForKey:@"latitude"],[parameter valueForKey:@"longitude"]];

    NSLog(@"JSON String: %@",jsonString);
    //NSLog(@"Request URL:  %@", requestString);
    NSData *jsonData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]];


    NSURL *url = [NSURL URLWithString:requestString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [request setHTTPBody: jsonData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
    NSString *strAuth = [NSString stringWithFormat:@"wfkey %@:%@",kAppID,kAppKey];
    [request setValue:strAuth forHTTPHeaderField:@"Authorization"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(theConnection) {
        webData = [[NSMutableData alloc] init];
    }
    else {
        NSLog(@"theConnection is NULL");
    }
}

-(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
{
    //    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //    [appDelegate hideProgressHUD];
    NSLog(@"didFailWithError: %@",[error description]);
    //    [CommonUtils webserviceError];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading Chat");
    NSError *jsonError = nil;
    NSDictionary *dictionaryJSON = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:&jsonError];
    NSLog(@"Response: %@",dictionaryJSON);
    [[NSNotificationCenter defaultCenter] postNotificationName:kChatSuccessNotification object:nil userInfo:dictionaryJSON];
}

@end  

网络服务电话:   viewcontroler1

NSMutableDictionary *dictRequest = [[NSMutableDictionary alloc] init];
    [dictRequest setValue:@"54783e4014e5f50007d50214" forKey:@"uid"];
    //[dictRequest setValue:@"ed9d61a838ae8ac83d5a7cabbc4e5779" forKey:@"app_key"];

    NSString *requestString = [CommonUtils encodeDictionary:dictRequest];
    // NSString *requestString = [CommonUtils getJSONString:dictRequest withPrettyPrint:NO];
    NSLog(@"requestString: %@",requestString);

    WebserviceForChat *webserviceChat = [[WebserviceForChat alloc] init];
    [webserviceChat callChatWebservice:requestString];  

请帮我解决这个问题。

0 个答案:

没有答案