第一種方式:
//構建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];
}