NSString* url = [NSStringstringWithFormat:@"%s?mt=1&qt=6045&mobilekey=%@",gURLHEADER.getData(), [[UIDevice currentDevice]uniqueIdentifier]];
NSURL *c_url = [NSURL URLWithString:url];
NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc]initWithURL:c_url];
[postRequest setHTTPMethod:@"POST"]; [postRequestsetValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[postRequest setTimeoutInterval:60];
NSHTTPURLResponse* response = nil;
NSData *resData = nil;
NSInteger code = 0;
int index = 0;
while (index < 2)
{
resData= [NSURLConnection sendSynchronousRequest:postRequestreturningResponse:&response error:nil];
code = [response statusCode];
if (code == 200)
{
break;
}
index++;
}
[postRequest release];
if (resData)//服务器返回的数据
{
//用xml方式解析resData
}
本文展示了一个使用Objective-C进行iOS应用开发中的同步网络请求示例。通过NSURLConnection发送POST请求到服务器,并处理响应数据。此外,还展示了如何设置请求头、超时时间和错误处理。
5664

被折叠的 条评论
为什么被折叠?



