本文介绍了Objective-C : 使用 Socket 发布/获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用任何已解释的 HTTPClient 发送 post/get 请求 此处此处.但是现在我有一个流套接字连接到远程服务器,我想使用这个套接字发送一个发布请求.

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.

我以前从未这样做过,坦率地说,我从未考虑过通过套接字进行 POST/GET 的概念.但是我的谷歌搜索让我找到了this,其中通过java套接字..

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..

我正在使用 Apple 提供的 CFSocket 库.无论如何在Objective-C中使用套接字进行POST/GET?

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

推荐答案

我已经能够使用普通的 CFSocket 执行 GET/POST.

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

现在在您的套接字回调函数中,您可以读取 http 响应..希望这会对某人有所帮助..

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

这篇关于Objective-C : 使用 Socket 发布/获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:46
查看更多