Cocoa:NSURLConnection未尝试HTTP请求 [英] Cocoa: NSURLConnection not attempting an HTTP Request

查看:93
本文介绍了Cocoa:NSURLConnection未尝试HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NSURL,NSURL [Mutable]请求,NSURLConnection与我的iPhone应用程序取得了重大成功。当试图编译一个独立的Cocoa应用程序时,10行程序做出一个简单的HTTP请求,有零编译器错误或警告。该程序编译良好,但HTTP请求永远不会对我的Web服务器(我运行一个tcpdump,并观看Apache日志并行)。当我在iPhone应用中运行非常类似的代码,基本上复制/粘贴为邪恶,所有的作品都是金色的。



为了简单起见,我将代码中的obj声明保存在NSURLConnection的代理中。我也将以下内容传递给gcc:



gcc -o foo foo.m -lobjc -framework可可



感谢任何洞察。

  #import< Cocoa / Cocoa.h> 

int main(int argc,char * argv [])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * urlstr = @http://tmp/test.php;
[NSApplication sharedApplication];
NSObject * obj = [[NSObject alloc] init];
NSURL * url = [NSURL URLWithString:urlstr];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];

if([request isKindOfClass:[NSMutableURLRequest class]])
NSLog(@request is type of NSMutableURLRequest);


[request setHTTPMethod:@GET];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:obj
startImmediately:YES];

if(connection)
NSLog(@我们有一个连接。

[pool release];
return 0;

}

解决方案<



第一,你真的不需要这么做链接到Cocoa为此,只是链接到Foundation框架是好的。此外,由于您不需要连接到Window Server,您可以摆脱[NSApplication sharedApplicaiton]调用。如果你只是一个简单的,控制台测试应用程序开始,使用你现在和添加这个之前你的[pool realease]调用:



[[NSRunLoop currentRunLoop] run];



但请注意,这将阻止,实际上可能永远不会返回。在调用这个之前,你可以添加一个定时器,如果你想让你的代码实际上做一些事情在后台:)请参阅关于NSRunLoop的文档更多的使用方法。


I've had significant success with NSURL, NSURL[Mutable]Request, NSURLConnection with my iPhone applications. When trying to compile a stand alone Cocoa application, 10 line program to make a simple HTTP request, there are zero compiler errors or warnings. The program compiles fine, yet the HTTP Request is never made to my web server (I'm running a tcpdump and watching Apache logs in parallel). When I run very similar code in an iPhone app, essentially copy/pasted as evil as that is, all works golden.

I kept the code for the 'obj' declaration in the delegate to NSURLConnection out of this code snippet for the sake of simplicity. I'm also passing the following to gcc:

gcc -o foo foo.m -lobjc -framework cocoa

Thanks for any insight.

#import <Cocoa/Cocoa.h>

int main (int argc, char *argv[])
{    

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * urlstr = @"http://tmp/test.php";
[NSApplication sharedApplication];
NSObject *obj = [[NSObject alloc] init];
NSURL *url = [NSURL URLWithString: urlstr];      
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    if([request isKindOfClass:[NSMutableURLRequest class]])
    NSLog(@"request is of type NSMutableURLRequest");


[request setHTTPMethod:@"GET"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
NSURLConnection *connection = [[NSURLConnection alloc]
                             initWithRequest:request
                     delegate:obj
                 startImmediately:YES];

if(connection) 
    NSLog(@"We do have a connection.");

[pool release];
return 0;

}

解决方案

The other poster pretty much answered this for you, but I thought I would just add a few things.

First, you don't really need to link to Cocoa for this, just linking to the Foundation framework is okay. Also, since you don't need a connection to the Window Server, you can get rid of the [NSApplication sharedApplicaiton] call. If you want just a simple, console test application to start with, use what you have now and add this before your [pool realease] call:

[[NSRunLoop currentRunLoop] run];

Please note, however, that this will block and may actually never return. Before calling this, you can add a timer if you want your code to actually do something in the background :) See the documentation on NSRunLoop for more ways to use this.

这篇关于Cocoa:NSURLConnection未尝试HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆