setHTTPBody是否需要编码类型

时间:2012-02-27 03:09:20

标签: iphone ios nsurlrequest

如果您通过方法参数中的NSData传递NSMutable数据类型,setHTTPBody是否需要编码类型?

基本上我有一个方法可以按照

的方式创建我的http正文包
- (void) constructRequest
{
//Create final packet
    NSMutableData * myConstructedMutableDataPacket = [[NSMutableData alloc] init];

    [myConstructedMutableDataPacket appendData:Sig];
    [myConstructedMutableDataPacket appendData:Ver];
//etc

//call connection method
 [self  [self VehicleSearchRequest:myConstructedMutableDataPacket];

} ];

}

然后在连接方法中我声明了我所有的请求。另外,我将NSMutableData对象myConstructedMutableDataPacket传递给我的连接类的NSData参数,如下所示。

- (IBAction)VehicleSearchRequest:(NSData *)postBodyData
{
   //all my connection crap

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0]; //if request dose not finish happen within 60 second timeout.

    [request setHTTPMethod: @"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody:postBodyData]; //Is this okay without encoding type?


    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

我想知道的是,如果我的 [request setHTTPBody:postBodyData]; 是可以的,因为我从方法本身的参数传递了NSData类型......

这就是我在许多地方看到的声明:

urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]];

0 个答案:

没有答案