第一种方式:
//构建http请求对象
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"www.example.org/services/VersionUpdate/getVersionInfo" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: [NSString stringWithFormat:@"%d", xmlstr.length] forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:data ];// [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
DMXProtocolFactory *haha = [[DMXProtocolFactory alloc]init];
//发送异步请求
// NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:haha];
//如果连接已经建好,则初始化data
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
// 发送同步请求, 这里得returnData就是返回得数据楽
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest
returningResponse:nil error:nil];
NSString *str = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
第二种方式:(用ASIFormDataRequest 或 ASIHTTPRequest 请求都可以 --- 但要配置好头信息)
// ASIFormDataRequest *reques = [[ASIFormDataRequest alloc]initWithURL:url];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
//设置ASIHTTPRequest代理
// request.delegate = aRequestModel.delegate;
//设置协议请求类型
[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1],@"RequestType",nil]];
// 设置请求方式
[request setRequestMethod:@"POST"];
// 设置请求头
[request setRequestHeaders:[NSMutableDictionary dictionaryWithDictionary:[DMXProtocolInteractiveEngine commonHeader:xmlstr.length]]];
// 设置post的数据
[request setPostBody:data];
[request startSynchronous];
NSError *errors = [request error];
NSString *response = nil;
if (!errors)
{
response = [request responseString];
}