2012-02-03 100 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

相關問題