Http事件监听器IOS(持久性http连接)

时间:2015-07-08 09:09:42

标签: ios objective-c http channel

是否有任何方法可以在IOS中创建http侦听器

HttpListener:我的意思是,我们将使用Post请求创建一个持久的http频道,服务器会将事件推送到该频道。我想创建一个等待服务器事件的频道。

-(void)httpPost:(NSString *)url andXml:(NSString *)xml{
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSURL *urls = [NSURL URLWithString:url];
    NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:urls];
    NSData *postData = [xml dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *username =[[ApplicationStorage applicationStorage] userName];
    NSString *password = [[ApplicationStorage applicationStorage] password];


    //HTTP Basic Authentication
    NSString *authenticationString = [NSString stringWithFormat:@"%@:%@", username, password];
    NSData *authenticationData = [authenticationString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *authenticationValue = [authenticationData base64Encoding];
    [request1 setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"];
    [request1 setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];

        [request1 setHTTPMethod:@"POST"];


    [request1 setHTTPBody:postData];

    NSError *error = nil;
    NSHTTPURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"httpPostOrPut >>>>>>>>> RESPONSE: %@",newStr);
   // [[NSRunLoop currentRunLoop] run];
}

我使用上面的帖子请求创建了频道。但是频道在创建后立即关闭

我在java中完成了这个

HttpResponse objBwResponse = bwPost(String.format(XML_CHANNEL_CREATION, generateChannelSetId()), getBWServerAddress() + CHANNEL_CREATION_URL);
            if (objBwResponse != null && objBwResponse.getStatusLine() != null && objBwResponse.getStatusLine().getStatusCode() == 200) {
                // success
                Log.w(LOG_TAG, "XSI channel created succesfully");
                InputStream content = objBwResponse.getEntity().getContent();

                onChannelConnected(gChannelId);
                processMessage(content);
            } else {
                // fail
                Log.w(LOG_TAG, "error in creating channel response: "+objBwResponse+" status code: "+(objBwResponse!=null?objBwResponse.getStatusLine():"response null"));

            }




private void processMessage(InputStream content) {
        String responseXML;
        Document documentXML;
        in = new BufferedInputStream(content);
        StringBuilder stringBuilder = new StringBuilder();
        try {
            int input;
            **while ((input = in.read()) != -1) {**
                stringBuilder.append((char) input);

                if (stringBuilder.indexOf(CHANNEL_EVENT_MESSAGE_END_STRING) != -1) {

谢谢

Amith

0 个答案:

没有答案