Tag: nsurlconnection

如何使用iOS可达性

我正在开发一个使用networking的iPhone应用程序。 iPhone通过HTTP请求与我的服务器通信,应该在WiFi和3G上工作。 我目前使用NSURLConnection initWithRequest向我的服务器发送asynchronous请求,并获得响应(但我很快将移动到ASIHTTPRequest库) 我明白,与这种types的应用程序(需要互联网连接的应用程序),我应该(必须)使用可达性。 在search网页并查看Apple的Reachability示例代码后,我仍然不明白一些基本的东西: 可达性的主要目的是什么? 在苹果的例子中,他们检测与主机,WiFi和3G的networking问题,并向用户呈现适当的消息。 这是Reachability的主要目的,向用户显示一条消息? 还是我需要使用它为更实际的情况? 例如,如果NSURLConnaction请求失败,我需要使用Reachability以某种方式重新发送请求? 可达性的正确使用是什么? 在应用程序启动时仅使用一个实例是否很常见,然后再听networking更改? 还是应该在每个networking请求之前检查自己的可达性状态? 是否足够使用reachabilityWithHostName还是我还需要reachabilityForLocalWiFi和reachabilityForInternetConnection ? 还有一件事 ,我明白苹果可以拒绝使用networking的应用程序,而不使用可达性。 我应该执行什么“ 必须 ”做的方法? 只是通知用户目前没有互联网就足够了吗?

iOS:我如何接收HTTP 401而不是-1012 NSURLErrorUserCancelledAuthentication

我有一个类似于下面的链接中描述的问题。 NSHTTPURLResponse statusCode返回零时,它应该是401 我使用[NSURLConnection sendSynchronousRequest:returningResponse:error:]从服务器获取数据。 当NSURLConnection接收到HTTP代码401时,它不会返回任何东西,而是返回-1012代码为-1012的错误对象。 -1012对应于NSURLErrorUserCancelledAuthentication 。 由于我必须parsingHTTP头,我需要得到原来的错误,而不是NSURLConnection做出来的。 有没有办法接收原始的401 HTTP数据包?

为什么总是返回-1

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; if([recievedData length]) [ recievedData setLength:0 ]; download_size =[response expectedContentLength]; } 我有这个代码。 download_size是NSInteger。 expectedContentLenght始终返回:-1。 也许有人知道为什么? 我试过使用很久,但效果是一样的。 感谢帮助。

POST请求不考虑NSMutableURLRequest超时间隔

我有以下问题。 在使用HTTP方法POST的NSMutableURLRequest ,为连接设置的超时间隔被忽略。 如果互联网连接有问题(代理错误,错误的DNS),约2-4分钟后url请求失败,但NSLocalizedDescription = "timed out";不会NSLocalizedDescription = "timed out"; NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out. 如果使用的http方法是GET它工作正常。 通过https连接是async 。 NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setTimeoutInterval:10]; //Set the request method to post [request setHTTPMethod:@"POST"]; if (body != nil) { [request setHTTPBody:body]; } // Add general parameters […]

如何发送asynchronousURL请求?

我想知道如何获得一个返回值1或0只是…从一个URL请求asynchronous返回。 目前我是这样做的: NSString *UTCString = [NSString stringWithFormat:@"http://web.blah.net/question/CheckQuestions?utc=%0.f",[lastUTCDate timeIntervalSince1970]]; NSLog(@"UTC String %@",UTCString); NSURL *updateDataURL = [NSURL URLWithString:UTCString]; NSString *checkValue = [NSString stringWithContentsOfURL:updateDataURL encoding:NSASCIIStringEncoding error:Nil]; NSLog(@"check Value %@",checkValue); 这工作,但是它阻止我的主线程,直到我从URL的回复,我如何设置它,这样做会在另一个线程,而不是主线程? 编辑:答案我结束upcalling我的function,这个效果不错:) [self performSelectorInBackground:@selector(shouldCheckForUpdate) withObject:nil];

使用Json在Objective C中发布数据

我正在尝试将数据发布到PHP Web服务。 我很熟悉在HTML中使用查询$ .post这样做,但我非常难倒试图在目标C.我尝试了几个博客和问题在stackoverflow上find。 我终于想出了以下代码: NSString *jsonRequest = [NSString stringWithFormat:@"{\"Email\":\"%@\",\"FirstName\":\"%@\"}",user,fname]; NSLog(@"Request: %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"http:myurl…"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: requestData]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; […]

如何使用sendAsynchronousRequest:queue:completionHandler:

两部分问题 第一部分:我试图创build一个ASSQL请求到我的数据库。 我目前正在做同步,但是我想学习两种方法,以更好地理解发生了什么事情。 目前我已经build立了像这样的同步呼叫。 – (IBAction)setRequestString:(NSString *)string { //Set database address NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8778/instacodeData/"]; // imac development //PHP file name is being set from the parent view [databaseURL appendString:string]; //call ASIHTTP delegates (Used to connect to database) NSURL *url = [NSURL URLWithString:databaseURL]; //SynchronousRequest to grab the data NSURLRequest *request = [NSURLRequest requestWithURL:url]; […]

iOS中的xmlparsing教程

嗨,我创build一个应用程序,必须打印从server.xml中的xml文件标签的值。我试着读谷歌search下的一些文档..但我找不到它…是否有人知道简单的教程或任何用于在我的应用程序中集成xmlparsing的示例代码。

NSURLConnection sendAsynchronousRequest无法从闭包中获取variables

我试图从一个PHP页面使用POST获取简单的文本响应。 我有以下代码: func post(url: String, info: String) -> String { var URL: NSURL = NSURL(string: url)! var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL) var output = "Nothing Returned"; request.HTTPMethod = "POST"; var bodyData = info; request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding); NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){ response, data, error in output = (NSString(data: data, encoding: NSUTF8StringEncoding))! } return output } 虽然这个代码不会抛出任何错误,但是当我这样打电话给它时: println(post(url, info: […]

在iOS上发送HTTP POST请求

我试图发送一个HTTP Post与我正在开发的iOS应用程序,但推送永远不会到达服务器,虽然我得到一个代码200作为响应(从URL连接)。 我从来没有得到从服务器的响应也没有检测到我的post(服务器检测到来自android的post) 我确实使用ARC,但将pd和urlConnection设置为较强。 这是我发送请求的代码 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",dk.baseURL,@"daantest"]]]; [request setHTTPMethod:@"POST"]; [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; NSString *sendString = @"<data><item>Item 1</item><item>Item 2</item></data>"; [request setValue:[NSString stringWithFormat:@"%d", [sendString length]] forHTTPHeaderField:@"Content-length"]; [request setHTTPBody:[sendString dataUsingEncoding:NSUTF8StringEncoding]]; PushDelegate *pushd = [[PushDelegate alloc] init]; pd = pushd; urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:pd]; [urlConnection start]; 这是我代表的代码 #import "PushDelegate.h" @implementation PushDelegate […]