Objective-C : Post/Get Request using Socket


I know how to send post/get request using any HTTPClient which are explained <a href="https://stackoverflow.com/questions/319463/can-i-make-post-or-get-requests-from-an-iphone-application" rel="nofollow">here</a> and <a href="https://stackoverflow.com/questions/5537297/ios-how-to-perform-a-http-post-request" rel="nofollow">here</a>. But now I <strong>have a stream socket connected to a remote server</strong> 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 <a href="http://www.java2s.com/Tutorial/Java/0320__Network/SendingaPOSTRequestUsingaSocket.htm" rel="nofollow">this</a>, where post is done via a java socket..

I am using <a href="https://developer.apple.com/documentation/corefoundation/cfsocket?language=objc" rel="nofollow">CFSocket</a> libraries provided by Apple..Is there anyway to do POST/GET using socket in Objective-C?


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


Refer <a href="http://shop.oreilly.com/product/9780596523190.do" rel="nofollow">Network Programming: Chapter 7 - iPhone SDK Application Development by Jonathan Zdziarski</a>. You can also refer <a href="http://cocoawithlove.com/2009/07/simple-extensible-http-server-in-cocoa.html" rel="nofollow">A simple, extensible HTTP server in Cocoa</a>.


  • UISearchBar 'prompt' Not Displaying Properly
  • How to change tomcat version from 5 to 7 for a given Eclipse Project
  • Can a filter access properties from my BaseController?
  • Scala code doesnt fetch s3 file
  • AngularJS: Hiding ng-message until hitting the form-submit button
  • How to place Compound drawable on top left of Text View
  • Catch failure in shell script
  • Manually call $scope.$apply raise error on ajax call - Error: [$rootScope:inprog]
  • dynamic height of chart of highchart
  • How to do multi-class image classification in keras?
  • How to run python script in Rstudio
  • How to create jQuery datatable with multiple child rows(nested table)?
  • Why doesn't JPA's FetchType.LAZY work?
  • Manifest marge error after migrating to androidX
  • Using downloaded font offline [duplicate]
  • Rxjs Observable Lifecycle
  • Java threads, green or native?
  • IOS - Facebook SDK fbDidLogin not called — initialize view controllers.
  • Performance benchmark of native android map vs webview map, what parameters can be included in the b
  • Order the result by best match on other field
  • How to upload an Image onto localhost server whilst path is stored in mysql database?
  • wxpython passing information, pointers?
  • How to read contents of a directory recursively in Linux Kernel?
  • Windows: How do I get the mode/access rights of an already opened file?
  • Questions related to Garbage Collector and finalize() method
  • Firestore: Version history of documents
  • Sample deviceQuery cuda program
  • Connect to a local database from phpmyadmin with R
  • What is the best way to cache and reuse immutable singleton objects in Java?
  • Computing the discrete fourier transform of audio data with FFTW
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • convert json to excel in java
  • Create/delete users from text file using Bash script