IOS Http Event Listener (Persistent http connection)

Is there a way to create an HTTP listener in IOS

HttpListener: I mean we will create a persistent http channel using Post request and it will send events to that channel. I want to create a channel listening for server events.

-(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];
}

      

I created a feed using the above request. but the channel is closed immediately after creation

I did it in 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) {

      

thank

Amith

+3


source to share





All Articles