标签: nsurlrequest

NSURLConnection泄漏?

我已经设置了一个nsurl,它从http中获取数据.当我运行仪器时,它说我有一个泄漏的NSFNetwork对象.

我如何释放theConnection在(无效)ButtonClicked?或者它会在稍后发布?

- (void)ButtonClicked {
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:KmlUrl]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:20.0f];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // receivedData is declared as a method instance elsewhere
        NSMutableData *receivedData = [[NSMutableData data] retain];
        [self setKMLdata:receivedData];
    } else {
        // inform the user that the download could not be made
    }
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to the receivedData
    // receivedData is declared as a method instance …
Run Code Online (Sandbox Code Playgroud)

memory-leaks memory-management objective-c nsurlconnection nsurlrequest

9
推荐指数
3
解决办法
1万
查看次数

如何在iphone中设置NSURLRequest的超时时间

当互联网连接断开连接时,我试图在15秒内断开或停止加载URL请求.我尝试使用NSURLRequest中的setTimeoutInterval,但这不起作用.任何人都知道如何解决这个问题?

objective-c nsurlrequest

9
推荐指数
1
解决办法
2万
查看次数

从iOS发送多部分POST并在PHP $ _POST中读取参数?

我正在尝试从iPhone/iPad发送多行帖子到php服务,问题是由于某种原因,POST内容类型似乎是application/x-www-form-urlenconded,(我发现使用Wireshark来解决这个问题)

这实际上是Wireshark POST数据包捕获的一小部分:

    **Content-Type: application/x-www-form-urlencoded\r\n**
    Content-Length: 324\r\n
        [Content length: 324]
    Connection: keep-alive\r\n
    \r\n
    [Full request URI: http://my.server.com/mobile/tools/authentication]
Line-based text data: application/x-www-form-urlencoded
    --0xKhTmLbOuNdArY\r\n
    Content-Disposition: form-data; name="login"\r\n
    \r\n
    hello@hello.com\r\n
    --0xKhTmLbOuNdArY\r\n
    Content-Disposition: form-data; name="password"\r\n
    \r\n
    somepassword\r\n
    --0xKhTmLbOuNdArY\r\n
    \r\n
    --0xKhTmLbOuNdArY--\r\n
Run Code Online (Sandbox Code Playgroud)

问题是在服务器中我试图从这个POST请求中读取登录名和密码变量,但是因为我认为php认为POST请求是不可能的x-www-form-urlencoded,所以如果我设置authentication.php为:

<?php
echo("<pre>")
print_r($_POST)
echo("</pre>")
?>
Run Code Online (Sandbox Code Playgroud)

我明白了:

<pre>Array\n
(\n
    [--0xKhTmLbOuNdArY\r\n
Content-Disposition:_form-data;_name] => "login"\r\n
\r\n
hello@hello.com\r\n
--0xKhTmLbOuNdArY\r\n
Content-Disposition: form-data; name="password"\r\n
\r\n
somepassword\r\n
--0xKhTmLbOuNdArY\r\n
\r\n
--0xKhTmLbOuNdArY--\r\n
\n
)\n
</pre>
Run Code Online (Sandbox Code Playgroud)

这显然不好,因为如果我使用这个简单的html表单发送请求:

<FORM action="http://my.server.com/mobile/tools/authentication.php" method="post">
   <P>
   <LABEL for="login">E-mail: </LABEL>
             <INPUT type="text" name="login"><BR>
   <LABEL for="password">pass: …
Run Code Online (Sandbox Code Playgroud)

php post objective-c nsurlrequest ios

9
推荐指数
1
解决办法
2万
查看次数

解决NSURL的重定向?

我有一个缩短的URL(例如t.co/12345678).我想解决重定向而无需通过NSURLConnection加载整个页面.显然,我可以通过发送NSURLConnection并等待响应来获取它,但这会先加载整个页面.我只是在寻找重定向网址.这可能吗?如果是这样,怎么样?

=============

编辑:对于后人,这是amleszk建议的解决方案.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:shortenedURL
                                         cachePolicy:NSURLRequestReloadIgnoringCacheData
                                     timeoutInterval:15.0f];

[request setHTTPMethod:@"HEAD"];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                           NSURL *resolvedURL = [httpResponse URL];
                           NSLog(@"%@", resolvedURL);
                       }];
Run Code Online (Sandbox Code Playgroud)

objective-c nsurl nsurlconnection nsurlrequest ios

9
推荐指数
1
解决办法
3511
查看次数

如何使用2个参数进行POST NSURLRequest?

我想添加2个参数NSURLRequest.有没有办法或者我应该使用AFnetworking?

post objective-c nsurlrequest ios

9
推荐指数
1
解决办法
2万
查看次数

NSURLConnection,NSURLRequest,不受信任的证书和用户身份验证

大家早上好,

我一直在尝试编写一个从需要身份验证的远程Web服务执行某些GET的应用程序.我的主要问题是这些远程服务器中的大多数(并且有很多远程服务器)没有有效的证书.我有代码接受无效的证书和代码,以正确的uname&pass(下面)来响应挑战.我遇到的问题是让两人一起玩.我似乎无法找到一种方法来发送挑战NSURLCredential或正确链接回调的方法.当我试图将它们连接起来时,我无法NSURLRequest接到didReceiveAuthenticationChallenge两次电话.

任何想法将不胜感激!

认证代码......

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{   
    if(!hasCanceled){
        if ([challenge previousFailureCount] == 0) {
            NSURLCredential *newCredential;
            newCredential=[NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
            [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        } 
        else {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
            NSLog(@"Bad Username Or Password");
            badUsernameAndPassword = YES;
            finished = YES;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa nsurlconnection nsurlrequest nsurlcredential

8
推荐指数
1
解决办法
3821
查看次数

使用NSURL请求时网址错误

我试图在iPhone 4.0 SDK中请求这种URL(访问令牌略有修改,因为你真的不需要看到它):

https://graph.facebook.com/me?sdk=ios&sdk_version=2&access_token=123902817987|8cb9e8408d2685cef853cd80.9-747882379|UGu5NvcAHiXuGEGzkq&format=json&limit=40&until=1286619821
Run Code Online (Sandbox Code Playgroud)

但是我得到了这样的信息:

Failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x9e657a0 {NSUnderlyingError=0x9e656a0 "bad URL", NSLocalizedDescription=bad URL}
Run Code Online (Sandbox Code Playgroud)

当我在Safari或Chrome中复制并粘贴时,它可以正常工作.我试着按照这里的建议进行更换|,但不起作用.%|

从终端询问它看起来像这样:

curl https://graph.facebook.com/me/statuses?sdk=ios&sdk_version=2&access_token=9999955817987|8ab9e8408d2685cef.3-747882379|UGuxWDuM&format=json&limit=40&until=1286619821
[1] 16190
[2] 16191
[3] 16194
[4] 16195
[5] 16196
[2]   Done                    sdk_version=2
[4]-  Done                    format=json
$ -bash: UGu5NvcAHiXuGEGzkq3KP8xWDuM: command not found
-bash: 8ab9e8408d2685cef853cd80.3-747882379: command not found
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
[1]   Done                    curl https://graph.facebook.com/me/statuses?sdk=ios
[3]-  Exit 127                access_token=9999955817987 | 8ab9e8408d2685cef.3-747882379 | …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsurlrequest ios4

8
推荐指数
2
解决办法
2万
查看次数

NSURLRequest:如何将httpMethod"GET"更改为"POST"

GET方法,它工作正常.

网址:http://myurl.com/test.html?query = id = 123& name = kkk


我没有POST方法的概念.请帮我.

我如何将GET方法转换为POST方法?


网址:http://testurl.com/test.html

[urlRequest setHTTPMethod:@"POST"];

iphone post nsurlrequest ios

8
推荐指数
2
解决办法
2万
查看次数

iOS Twitter NSURLErrorDomain代码= -1012

我试图通过在我的应用程序中使用twitter阅读来获取用户的联系方式.我在github上发现这个项目看起来真的很棒.我只遇到一个问题.如果我使用来自Twitter的新应用消费者密钥和消费者密钥代码运行它会给我一个错误:

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x6898e80 {NSErrorFailingURLKey=https://api.twitter.com/oauth/request_token, NSErrorFailingURLStringKey=https://api.twitter.com/oauth/request_token, NSUnderlyingError=0x68980e0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
Run Code Online (Sandbox Code Playgroud)

控制台中的输出是:

2013-01-16 17:55:24.367 DMTwitterOAuth[3411:c07] current status = Prompt for user data and request token to server
2013-01-16 17:55:24.371 DMTwitterOAuth[3411:c07] current status = Requesting token for current user's auth data...
2013-01-16 17:55:25.433 DMTwitterOAuth[3411:c07] current status = Token received from server
Run Code Online (Sandbox Code Playgroud)

在我得到警报视图后,它立即说它遇到了一个NSURLErrorDomain.

另一方面,如果我使用来自另一个Twitter应用程序的其他代码运行它,它会工作,向我提供有关用户的所有详细信息.我只是无法理解为什么它可以使用某些键,而对于其他键则不然.

twitter nsurlrequest ios nsurlerrordomain

8
推荐指数
1
解决办法
1万
查看次数

NSURLConnection和多个异步请求 - 是否正在处理正在传输的数据?

我有一个NSArray链接.我想通过在线文章提取器API(清除读取)解析它们,并将每个文章(一些HTML)的结果返回给我NSString.

我的问题源于这样一个事实,即我的数组中有100个URL,我循环遍历数组,将每个项目射入API,并在JSON中返回一些结果.这是NSURLConnection异步激活100次调用.

我不确定这是不是一个问题,但当我给它100个URL(真正的字符串,没有一个是nil)时,返回的数据通常具有JSON键的空值(当它们不应该时),或者回来的数据是nil.还有一堆重复.

我应该比现在更好地处理多个异步连接吗?如果是这样,怎么样?

api objective-c nsurlconnection nsurlrequest ios

8
推荐指数
1
解决办法
4587
查看次数