Objective C - 带守护进程的HttpRequest

时间:2013-05-10 13:29:52

标签: objective-c httprequest daemon

我在Objective C中创建了一个守护进程,在搜索USB设备后,必须向网址发送HttpRequest。

我已经尝试过浏览器,主机可以访问。

对于创建一个守护进程,我已经使用了Foundation框架,并且我试图用这种方式创建一个HTTP请求:

NSURLRequest *eventRequest = [NSURLRequest requestWithURL:_urlRequest cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];

self.theConnection=[[NSURLConnection alloc] initWithRequest:eventRequest delegate:self];

if (self.theConnection) {
    self.receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
    NSLog(@"Connection Problem");
}

但在这种情况下我有两个问题:

  1. 请求未发送。
  2. 守护程序结束,不等待HttpRequest的响应。
  3. 我该如何解决这个问题? 是否存在另一种制作HTTP请求并获得响应的方法?

    问候。

3 个答案:

答案 0 :(得分:0)

许多人发现AFNetworking框架(来自https://github.com/AFNetworking/AFNetworking的GIT克隆)使HTTP交互更加容易。

具体来说,AFNetworking getPath:postPath:方法接受pathparameters字典,然后是两个块,一个用于success,另一个用于{ {1}}。就这样

failure

创建AFNetworking客户端也很简单。您可以创建一个简单的HTTP客户端,甚至可以创建OAuth2客户端:

{ [self getPath: @"users"
     parameters: @{ @"api_key" : @"AppAPIKeyForServer" }
        success: ^(AFHTTPRequestOperation *operation, NSDictionary *json) {
          NSArray *users = json[@"users"];
          ....
        }
        failure: ...]; }

至于你的守护进程需要。您应该能够让守护程序保留指向AFNetworking客户端的指针,然后重复请求。

请注意,可在https://github.com/AFNetworking/AFOAuth2Client

找到OAuth2客户端

答案 1 :(得分:0)

如果您正在使用NSURLConnection,则发送异步请求并使用委托,

// Create a Request and Connection
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR URL"]];
NSURLConnection *theConnection= [NSURLConnection connectionWithRequest:request delegate:self];
// Start Request
[theConnection start];

委派方法

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"did fail");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"did receive data");
    [self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"did receive response ");
    self.receivedData = [NSMutableData dataWithCapacity:0];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"did finish loading");
    [connection release];
}

答案 2 :(得分:0)

使用正常的方式使用NSURLConnection Framework发送http消息不运行我已经尝试了CoreFoundation.Framework来做到这一点:

-(void)implementaZioneChiamata{

for (int i = 0; i < [_arrayOfSerial count]; i++) {

    CFStringRef urlFirst = CFSTR("http://192.168.30.9:88/virtualBadge.aspx?token=");
    CFStringRef token =(CFStringRef)_codicePWD;
    CFStringRef urlSecond = CFSTR("&serial=");
    CFStringRef serial = (CFStringRef) [_arrayOfSerial objectAtIndex:i];
    NSLog(@"URL: %@%@%@%@", urlFirst,token,urlSecond,serial);

    CFStringRef strs[4];

    strs[0] = urlFirst;
    strs[1] = token;
    strs[2] = urlSecond;
    strs[3] = serial;

    CFArrayRef urlArray = CFArrayCreate(kCFAllocatorDefault, (void*)strs, 4, &kCFTypeArrayCallBacks);

    CFStringRef urlCompleta = CFStringCreateByCombiningStrings(kCFAllocatorDefault, urlArray, CFSTR(""));
    NSLog(@"Url Completa: %@",urlCompleta);

    CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, urlCompleta, NULL);
    CFStringRef headerFieldName = CFSTR("host");
    CFStringRef headerFieldValue = CFSTR("192.168.30.9");


    CFStringRef requestMethod = CFSTR("GET");
    CFHTTPMessageRef myRequest =CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL,
                               kCFHTTPVersion1_1);
      CFHTTPMessageSetHeaderFieldValue(myRequest, headerFieldName, headerFieldValue);
       CFDataRef mySerializedRequest = CFHTTPMessageCopySerializedMessage(myRequest);

    NSString *messReq = [[NSString alloc] initWithData:(NSData*)mySerializedRequest encoding:NSASCIIStringEncoding];

    CFStream


    NSLog(@"Serialized Request: %@", messReq);

}

在代码的第一部分我创建了URL。在此之后我使用CFHTTPMessageRef来创建HttpRequest.But现在出现了问题:

一旦我有HttpMessage发送它,我需要创建CFStreamRef来发送消息。开始我的想法,我创建流我如何发送消息?使用CFStream API?

好的,在打开流之后,Guy Tnx全部寻求帮助请求就像这样发送添加de stream。

CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest);

CFReadStreamOpen(myReadStream);

Tnx all !!