Tag: nsurlconnection

如何在iOS中在后台运行NSURLConnection

我正在与待办事项列表组织者的应用程序,用户添加注释。 我正在使用coredata数据库来存储笔记。 由于我提供同步function,我正在parsingJSON数据到服务器,并从服务器获取JSON数据。 我正在使用NSURLConnection API及其委托function – (void)pushData { loop through the notes array and send notes 1 by one [[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:jsonData]; m_dataPush = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; [m_dataPush start]; } – (void)connectionDidFinishLoading:(NSURLConnection *)connection { Process response from server, save to core DB and again pushData if any modified and […]

使用Cocoa Touch从服务器上传和下载数据?

如何从Cocoa Touch的服务器上传/下载数据? 这是我到目前为止… -(void)uploadSchedule:(id)sender { NSData *content = [NSData dataWithContentsOfFile:self.dataFilePath]; NSString *stuff = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding]; NSURL *url = [NSURL URLWithString:@"http://thetis.lunarmania.com"]; NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc]initWithURL:url]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:[stuff dataUsingEncoding:NSASCIIStringEncoding]]; NSLog(@"great success!"); } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // this method is called when the server has determined that it // has enough […]

上传带有参数的文件(图像,pdf)到服务器

我想上传1pdf文件和图片数组。 我可以上传1pdf和2jpg文件,如果jpg文件超过两个我得到的消息: 错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(cocoa错误3840.)”(没有值。)UserInfo = 0x174268000 {NSDebugDescription =没有值。} 为什么发生这种情况? 另外我用POSTMANtesting了服务器,它工作的很好。 NSURL *URL = [NSURL URLWithString:constFileUploadURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setHTTPShouldHandleCookies:NO]; [request setTimeoutInterval:60]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"0xLhTaLbOkNdArZ"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"–%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString […]

connectionDidFinishLoading:不使用[NSURLConnection sendAsynchronousRequest:queue:completionHandler:

我想用NSMutableURLRequest启动一个请求,当[NSURLConnection sendAsynchronousRequest的答案:获得一个好的状态码。 当我单独启动包含NSMutableURLRequest的executeForStatusOkMetod时,它完美地工作。 但是,如果我用[NSURLConnection sendAsynchronousRequest:]启动executeForStatusOkMetod , connectionDidFinishLoading:函数永远不会被调用。 这里是主要的代码: NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; request.timeoutInterval = 10; [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description); if ((long)[httpResponse statusCode] >= […]

如果方法使用NSURLConnection如何返回输出与块的asynchronous调用

我正在向服务器发送请求以使用NSURLConnection块方法获得响应。 我不能使用返回YES或在块中返回NO。 我怎样才能解决这个问题? -(BOOL)checkForValidUrl:(NSString *)url { __block int httpStatus=0; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]]; [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { httpStatus = [((NSHTTPURLResponse *)response) statusCode]; NSLog(@"httpStatus inside block:%d",httpStatus); } ]; return NO; }

如何从Web服务器获取cookie并将其保存在iOS应用程序中?

我想从http://mycompany.com/page1…http://mycompany.com/page2获取JSON …在web服务器端,它需要初始loginhttp://mycompany.com / login ,然后为用户维护一个cookie。 我怎么得到这个NSURLConnection行为,而不必每次都要求login? 这是使用NSURLCredential存储的非工作代码。 我是否需要在login时从web服务获取cookie,然后将其与以后的请求一起发送? 我一直在努力,所以你可以澄清一下你的答案。 – (IBAction)getJSON:(id)sender { NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user" password:@"pass" persistence:NSURLCredentialPersistenceForSession]; NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:@"myCompany.com" port:0 protocol:@"http" realm:nil authenticationMethod:nil]; [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace]; //////////GET JSON////////////// NSError *error; NSURL *url = [NSURL URLWithString:@"http://mycompany.com.jsonpage1"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } //I am NOT […]

在iOS中使用sendAsynchronousRequest:request API处理401状态码

我正在使用这种方式的API来login我的应用程序。 NSString *url = [NSString stringWithFormat:@"%@//login",xyz]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; if (!jsonData) { NSLog(@"Error creating JSON object: %@", [error localizedDescription]); } [request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [request setValue:APIKEY forHTTPHeaderField:@"X_API_KEY"]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:jsonData]; [NSURLConnection sendAsynchronousRequest:request // the NSOperationQueue upon which the handler block will be dispatched: […]

NSURLConnection错误代码(-1202,1012)

我正在使用NSURLConnection作为一个现场服务types的iOS应用程序,所以应用程序是现场服务types有超过50个用户,超过50个用户可能同时使用该应用程序,这就是为什么有超过50或60请求来到服务器。 现在我的问题是我经常收到以下两个错误,意味着每一个用户一天可能会面对这个错误超过5次。 所以这对我来说是一个挑战。 错误代码: -1202 NSURLErrorServerCertificateUntrusted -1012 NSURLErrorUserCancelledAuthentication 我搜查了很多,我发现他们是服务器相关的错误,但仍然没有任何解决scheme如何解决这个问题。 请帮我我如何解决这个NSURLConnection错误( – 1202 NSURLErrorServerCertificateUntrusted和-1012 NSURLErrorUserCancelledAuthentication )的问题。 提前致谢。

NSMutableData响应string

我想上传图片到yFrog工作正常,但我只想抓住响应的url。 当我使用NSURLConnection方法 – (void) connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; NSString* responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; // NSString *url = [webData valueForKey:@"mediaurl"]; NSLog(@"result: %@", responseString); } 我得到这个作为我的回应string result: <?xml version="1.0" encoding="UTF-8"?> <rsp stat="ok"> <mediaid>hszuhsp</mediaid> <mediaurl>http://yfrog.com/hszuhsp</mediaurl> </rsp> 正如你可以看到我的封锁代码,我试图给我的NSMutableData给我的关键@“mediaurl”的价值刚刚崩溃。 我认为这应该是相对容易的,但由于某种原因,我只是无法弄清楚如何从响应中获取URL。 任何帮助将不胜感激。 谢谢

如何在iOS中发送NTLM请求

我使用json(NSJSONSerialization)和NSURL进行服务器客户端通信。 直到现在它工作正常,但现在NTLM安全已经在服务器上实现。 任何人都可以告诉我如何发送与iOS的NTLM请求? 谢谢