如何在ios中发布html数据时获取http post方法的响应?

时间:2015-06-24 13:58:50

标签: ios

我的代码在这里...... 没有收到所有的HTML数据....所以PLZ回复我:sanandiyavipul@gmail.com

NSString *get_value_key=[NSString stringWithFormat:@"personen=%@&eigenaar=%@&soortwoning=%@&beveiliging=%@&soortdak=%@",[filter_ary4 objectAtIndex:[pickerView4 selectedRowInComponent:0]],[filter_ary7 objectAtIndex:[pickerView7 selectedRowInComponent:0]],[filter_ary8 objectAtIndex:[pickerView8 selectedRowInComponent:0]],[filter_ary9 objectAtIndex:[pickerView9 selectedRowInComponent:0]],[filter_ary10 objectAtIndex:[pickerView10 selectedRowInComponent:0]]];



NSData *postData = [get_value_key dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://media.komparu.com/inboedelverzekering/10045724/5193"]];


[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"text/html; charset=UTF-8" forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody:postData];

// NSError *requestError;
NSURLResponse *urlResponse =nil;

response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil];

NSString *requestReply = [[NSString alloc] initWithBytes:[response1 bytes] length:[response1 length] encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);

1 个答案:

答案 0 :(得分:0)

如果您想要响应,则它位于变量:urlResponse

如果您希望html状态代码将响应转换为NSHTTPURLResponse并访问statusCode属性。

或者只是创建NSHTTPURLResponse而不是NSURLResponse并直接访问。

NSError *error;
NSHTTPURLResponse *urlResponse;
response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSInteger statusCode = urlResponse.statusCode;
NSLog(@"statusCode: %i", statusCode);
if (response1) {
    // process data
}
else {
    // Log the response and error description
    NSLog(@"urlResponse: %@", urlResponse);
    NSLog(@"error: %@", error);
}