0

我想从我的 iOS 应用程序连接到 Web 服务。以前我的网址是http://<domain name>/mobilews/mobilews.asmx. 最近我改为http://<domain name>/mobilewstest/mobilews.asmx

我已将我的 URL 放入 info plist 文件中。但是在我将其更改为新 URL 后,我无法登录。

NSString *urlString = [NSString stringWithFormat:@"%@%@",serverURL,[queryString  stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];


NSHTTPURLResponse *response ;

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

int statusCode = [((NSHTTPURLResponse *)response) statusCode];`

这里returnData变成nillstatusCode是0。但是urlString当它在浏览器中给出时,它已成功登录到Web服务。

4

1 回答 1

0
    NSURL *url = [NSURL URLWithString:@"YOUR URL HERE"];
    NSError *connectionError = nil;
    NSData *inData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&connectionError];
    NSInteger code = [connectionError code];
    if (code != 0)
    {
        NSString *locDesc = [NSString stringWithString:[connectionError localizedDescription]];
        NSString *locFail = [NSString stringWithString:[connectionError localizedFailureReason]];
        NSLog(@"Error: %d %@ %@", code, locDesc, locFail);
    }
    else if ([inData length] == 0)
    {
        NSLog(@"No data");
    }
    else{
    NSData *resultData  = [NSData dataWithContentsOfURL:url];
    NSString *responseString = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
    }

我看到你发送了一个同步请求......试试这个更简单的方法。

于 2013-02-27T08:17:46.753 回答