2012-02-03 101 views
0

我打电话从我的iPhone应用程序(的iOS5)这样的本地托管的.NET Web服务:通过NSURLConnection问题调用Webservice,每次都不会调用?

(IBAction)btnCallService:(id)sender { 
    [XYZActivity startAnimating]; 
    // So I could show activity on my main UI thread. 
    [self performSelector: @selector(CallXYZSService) 
       withObject: nil 
       afterDelay: 0]; 
    } 

(void) CallXYZSService 
{ 
    NSURL *url = [NSURL URLWithString: @"http://localwinhost/JSON_Service.asmx/GetFunction1"]; 


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:1 timeoutInterval:30]; 
    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; 

    [request setHTTPMethod:@"POST"]; 

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"close" forHTTPHeaderField:@"Connection"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody: requestData]; 
    //[req setHTTPBody: [postStr dataUsingEncoding:NSUTF8StringEncoding]]; 

    myConn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (myConn) { 
     webData = [NSMutableData data]; 
    } 
    else 
    { 
     NSLog(@"Connection Failed"); 
    } 

} 

} 


-(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(@"Service Failed"); 
} 

现在我的问题是有时它调用的服务,我recive数据,有时它doesn'tand没有任何反应。 ...甚至没有超时错误...

我在做什么错在这里,我打电话给我的web服务异步...问题是它甚至不去任何其他委托方法,只准备请求,初始化连接,然后什么也没有发生......

是否有与以前的连接到w ebservice?至于测试,我在同一个服务器上的同一服务上调用了2,3个不同的函数......但是这些都发生在不同的视图控制器上......所以我启动了新的连接并在connectionDidFinishLoading方法中将连接设置为nil。

可以任何一个PLZ帮助我在这里,如何确保我总是得到服务的回应有效的回应或一些错误....或超时.... 如果我测试相同的服务在MAC Safari,它总是被调用,没有任何问题...... !!!

感谢, 特立独行


我已经改变了代码如下

I have changed the code as below NSString *jsonRequest_NE = [NSString stringWithFormat:@"ID=%@&Password=%@",strID,strPwd]; 
NSString *jsonRequest = [jsonRequest_NE stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSLog(@"Request: %@", jsonRequest); 

NSString *myURL = @"http://localwinhost/JSON_Service.asmx/GetFunction1"; 

NSString *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

,但还是一样beahviour,有时它的工作原理有时它只是不..... 我搜索了很多...但仍然无法找到适当的解决方案... 任何一个PLZ帮助我hereeeeeeeeeeee!

回答

0

确保您的jsonRequest变量中没有任何非UFT8字符。
让我知道你是如何管理它的。

+0

我编码一切正常(我认为),但我仍然得到“连接丢失”的相同问题。 FUnny的事情有时是相同的代码工作,我开始得到适当的回应,有时它根本没有。 – 2012-02-06 06:55:50

相关问题