Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 正确使用HTTP协议和参数_Objective C_Ios_Http_Client Server_Asihttprequest - Fatal编程技术网

Objective c 正确使用HTTP协议和参数

Objective c 正确使用HTTP协议和参数,objective-c,ios,http,client-server,asihttprequest,Objective C,Ios,Http,Client Server,Asihttprequest,我正在开发一个iPhone短信应用程序,使用HTTP从客户端到服务器发送和接收数据 为了让客户端与服务器通信,客户端创建HTTP请求并用相关参数填充它 每个客户端请求都包含以下参数: 命令 clientId用于标识客户端的唯一id 某些特定于命令的数据用于命令的额外数据 1将这些参数添加到HTTP POST请求的正确方法是什么 到目前为止,我做了如下工作: - (void)sendTextMessage:(NSString *)text forClient:(NSString *)clientI

我正在开发一个iPhone短信应用程序,使用HTTP从客户端到服务器发送和接收数据

为了让客户端与服务器通信,客户端创建HTTP请求并用相关参数填充它

每个客户端请求都包含以下参数: 命令 clientId用于标识客户端的唯一id 某些特定于命令的数据用于命令的额外数据

1将这些参数添加到HTTP POST请求的正确方法是什么

到目前为止,我做了如下工作:

- (void)sendTextMessage:(NSString *)text forClient:(NSString *)clientId
{
    NSString *url = SERVER_URL;

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"cmd=%@&clientid=%@&msg=%@", 
                       @"sendmsg", 
                       clientId,
                       text] dataUsingEncoding:NSUTF8StringEncoding]];


    [request setHTTPBody:body];

    // send the request...
}
2此方法工作正常,但我不确定这是我应该如何使用HTTP的,使用HTTP正文发送这些参数可以吗

无论如何,正如我所说,它工作得很好,所以我没有问题,直到我在我的应用程序中添加了发送图片的选项。。。 这让事情变得有点复杂,图像放在哪里,命令参数字符串放在哪里

因此,我开始使用ASIHTTPRequest库,以便于发送图片。 现在的问题是,3如何处理命令参数?我在哪里添加它们

我又一次将它们添加到URL本身,这可能不是HTTP的一个好用途,另一个问题是我无法将文本参数添加到URL,因为它有空格。。 因此,我添加了如下文本参数:

 [request setPostValue:text forKey:@"msgText"];
我甚至不知道这意味着什么?这是否添加到HTTP头中?还是尸体

以下是我如何使用ASIHTTPRequest发送带有文本的图像:

很抱歉问了这么长的问题,最后我有4个问题,黑体字: 问题1、2是一般HTTP使用问题
问题3、4是特定的,因为HttpRequest使用问题

您一次问的问题太多;试着一次只问一个问题,这听起来像是一个很好的提示:
- (void)sendMsgText:(NSString *)text andImage:(UIImage *)image forClient:(NSString *)clientId
{
    NSString *url = SERVER_URL;

    // add the command parameters to the url
    NSString *cmd = [NSString stringWithFormat:@"cmd=%@&clientid=%@", 
                        @"sendmsg", 
                        clientId];

    NSString *newUrl = [NSString stringWithFormat:@"%@?%@", url, cmd];

    // create HTTP request
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:newUrl]];

    // add the text
    [request setPostValue:text forKey:@"msgText"];
    // add the image
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    [request addData:imageData withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];

    [request startAsynchronous];
}