1

I know how to send post/get request using any HTTPClient which are explained here and here. But now I have a stream socket connected to a remote server and I want to send a post request using this socket.

I never done that before and frankly never considered a concept of POST/GET via socket. But my googling led me to this, where post is done via a java socket..

I am using CFSocket libraries provided by Apple..Is there anyway to do POST/GET using socket in Objective-C?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167

2 Answers2

8

Well I have been able to do GET/POST using plain CFSocket.

-(bool) sendHTTPHeader{
    NSMutableString* requestString  =   [[NSMutableString alloc] init];
    NSString *pathString            =   [NSString stringWithFormat:@"/remote_function.jsp?argument1=%@&argument2=%@",arg1,arg2 ];
    [requestString appendFormat:@"GET %@ HTTP/1.x\r\n",pathString];
    [requestString appendFormat:@"HOST:%@",your_host_here];
    [requestString appendFormat:@"User-Agent:any\r\n"];
    [requestString appendFormat:@"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n\r\n"];
    /*If there are more parameters requested by server, put them here..*/

    const char* sendString          =   [requestString UTF8String];

    CFDataRef  data                 =   CFDataCreate(NULL, (const UInt8*)sendString, strlen(sendString));
    CFSocketError err               =   CFSocketSendData(clientSocket, NULL, data, 0);
    if (err != kCFSocketSuccess) {
        NSLog(@"Error in sending data to the server");
        CFRelease(data);
        return FALSE;
    }
    [requestString release];
    CFRelease(data);
    return TRUE;
}

Now in your socket callback function you can read the http response..Hope this will help someone..

void socketCallback(CFSocketRef socket,CFSocketCallBackType type,CFDataRef address,const void *data,void *info){
    if (type == kCFSocketDataCallBack) {
        NSData *nsData              =   (NSData *)data;
        if ([nsData length] == 0) {
            NSLog(@"Connection dropped by remote socket");
            return;
        }
        //Control reaches here when there is a chunk of data to read
        CFDataRef df                =   (CFDataRef) data;
        int len                     =   CFDataGetLength(df);
        CFRange range               =   CFRangeMake(0,len);
        UInt8 buffer[len+1];

        if(len <= 0) {
            NSLog(@"No data read");
            return;        
        }
        memset((void*)buffer,0,sizeof(buffer));
        CFDataGetBytes(df, range, buffer);
        NSString *byteString            =   [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
        NSLog(@"byte string is : %@",byteString);
        [byteString release];
    }
}
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • Upvoted as this example fixed a problem for me (I had a syntax issue), however I had to tweak your request slightly - I needed a carriage return and line feed after the "HOST" entry. Just thought I'd mention it. I got a bad request error without it. – David Fulton Sep 28 '17 at 14:58
3

Refer Network Programming: Chapter 7 - iPhone SDK Application Development by Jonathan Zdziarski. You can also refer A simple, extensible HTTP server in Cocoa.

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • Thanks for your response friend, My question was about how to do GET/POST request using a plain CFSocket. I have already seen both of these links, which were very helpful. But there is nothing about post/get using socket in the second link. In the first link there is information about CFHTTP which looks promising..But I have completed my requirement using plain CFSocket. See my answer. I will accept my answer tomorrow as SO not allowing me to do it now – Krishnabhadra Nov 11 '11 at 09:54