IOS使用AFneting进行Post访问webservice
2016-01-07 16:04
477 查看
在iOS中只使用post方法访问时,一定要注意报文头部的拼接
- (NSString *)getTheSoapMsg:(NSString *)middleStr
{
NSString *string = [NSString
stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"%@"
"</soap:Body>"
"</soap:Envelope>", middleStr];
return string;
}
在拼接好报文之后,再使用afneting访问
-(void) soapTest
{
NSString *soapMsg=[self
getTheSoapMsg:@"<GetCarGPSList xmlns=\"http://tempuri.org/\" />"];
NSString *msgLength = [NSString
stringWithFormat:@"%ld", (unsigned
long)[soapMsg length]];
NSString *space=nameSpace;
NSString *methodname=@"GetCarGPSList";
NSString *soapaction=[NSString
stringWithFormat:@"%@%@",space,methodname];
NSURL *url = [NSURL
URLWithString:endPoint];
AFHTTPClient *httpClient = [[AFHTTPClient
alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:endPoint
parameters:nil];//这里的parameters:参数就是你的第二个问题如何设置参数
NSDictionary *headField=[NSDictionary
dictionaryWithObjectsAndKeys:[url host],@"Host",
@"text/xml; charset=utf-8",@"Content-Type",
msgLength,@"Content-Length",
soapaction,@"SOAPAction",nil];
[request
setAllHTTPHeaderFields:headField];
//超时设置
[request
setTimeoutInterval:
30 ];
//访问方式
[request
setHTTPMethod:@"POST"];
//body内容
[request setHTTPBody:[soapMsg
dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [httpClient
HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSString * a=operation.responseString;
NSString * b=[a
stringByReplacingOccurrencesOfString:@"<"
withString:@"<"];
NSString * c=[b
stringByReplacingOccurrencesOfString:@">"
withString:@">"];
NSString *m = [self
getcontent:c :@"<GetCarGPSListResult>" :@"</GetCarGPSListResult>"];
parserObjects = [[NSMutableArray
alloc] init];
NSDictionary *xmlDoc = [NSDictionary
dictionaryWithXMLString:m];
NSArray *zz= [xmlDoc
valueForKey:@"Item" ];
for (int i=0; i<zz.count;i++) {
CarObject *object=[[CarObject
alloc] init];
NSDictionary *dic=[zz
objectAtIndex:i];
if ([dic objectForKey:@"_carno"]) {
//获取属性节点中的值
NSString *carno=[dic
objectForKey:@"_carno"];
object.carno=carno;
}
if ([dic objectForKey:@"_phoneno"]) {
//获取属性节点中的值
NSString *phoneno=[dic
objectForKey:@"_phoneno"];
object.phoneno=phoneno;
}
if ([dic objectForKey:@"_cartype"]) {
//获取属性节点中的值
NSString *cartype=[dic
objectForKey:@"_cartype"];
object.cartype=cartype;
}
if ([dic objectForKey:@"_depart"]) {
//获取属性节点中的值
NSString *depart=[dic
objectForKey:@"_depart"];
object.depart=depart;
}
if ([dic objectForKey:@"_worktype"]) {
//获取属性节点中的值
NSString *worktype=[dic
objectForKey:@"_worktype"];
object.worktype=worktype;
}
if ([dic objectForKey:@"_grouptype"]) {
//获取属性节点中的值
NSString *grouptype=[dic
objectForKey:@"_grouptype"];
object.grouptype=grouptype;
}
if ([dic objectForKey:@"_isonline"]) {
//获取属性节点中的值
NSString *isonline=[dic
objectForKey:@"_isonline"];
object.isonline=isonline;
}
[parserObjects
addObject:object];
}
}
failure:^(AFHTTPRequestOperation *operation,
NSError *error)
{
NSLog(@"请求失败=%@",error);
NSString *html = operation.responseString;
NSLog(@"请求失败返回的字符串~~~~~~~:%@",html);
}
];
[httpClient
enqueueHTTPRequestOperation:operation];
}
- (NSString *)getTheSoapMsg:(NSString *)middleStr
{
NSString *string = [NSString
stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"%@"
"</soap:Body>"
"</soap:Envelope>", middleStr];
return string;
}
在拼接好报文之后,再使用afneting访问
-(void) soapTest
{
NSString *soapMsg=[self
getTheSoapMsg:@"<GetCarGPSList xmlns=\"http://tempuri.org/\" />"];
NSString *msgLength = [NSString
stringWithFormat:@"%ld", (unsigned
long)[soapMsg length]];
NSString *space=nameSpace;
NSString *methodname=@"GetCarGPSList";
NSString *soapaction=[NSString
stringWithFormat:@"%@%@",space,methodname];
NSURL *url = [NSURL
URLWithString:endPoint];
AFHTTPClient *httpClient = [[AFHTTPClient
alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:endPoint
parameters:nil];//这里的parameters:参数就是你的第二个问题如何设置参数
NSDictionary *headField=[NSDictionary
dictionaryWithObjectsAndKeys:[url host],@"Host",
@"text/xml; charset=utf-8",@"Content-Type",
msgLength,@"Content-Length",
soapaction,@"SOAPAction",nil];
[request
setAllHTTPHeaderFields:headField];
//超时设置
[request
setTimeoutInterval:
30 ];
//访问方式
[request
setHTTPMethod:@"POST"];
//body内容
[request setHTTPBody:[soapMsg
dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [httpClient
HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSString * a=operation.responseString;
NSString * b=[a
stringByReplacingOccurrencesOfString:@"<"
withString:@"<"];
NSString * c=[b
stringByReplacingOccurrencesOfString:@">"
withString:@">"];
NSString *m = [self
getcontent:c :@"<GetCarGPSListResult>" :@"</GetCarGPSListResult>"];
parserObjects = [[NSMutableArray
alloc] init];
NSDictionary *xmlDoc = [NSDictionary
dictionaryWithXMLString:m];
NSArray *zz= [xmlDoc
valueForKey:@"Item" ];
for (int i=0; i<zz.count;i++) {
CarObject *object=[[CarObject
alloc] init];
NSDictionary *dic=[zz
objectAtIndex:i];
if ([dic objectForKey:@"_carno"]) {
//获取属性节点中的值
NSString *carno=[dic
objectForKey:@"_carno"];
object.carno=carno;
}
if ([dic objectForKey:@"_phoneno"]) {
//获取属性节点中的值
NSString *phoneno=[dic
objectForKey:@"_phoneno"];
object.phoneno=phoneno;
}
if ([dic objectForKey:@"_cartype"]) {
//获取属性节点中的值
NSString *cartype=[dic
objectForKey:@"_cartype"];
object.cartype=cartype;
}
if ([dic objectForKey:@"_depart"]) {
//获取属性节点中的值
NSString *depart=[dic
objectForKey:@"_depart"];
object.depart=depart;
}
if ([dic objectForKey:@"_worktype"]) {
//获取属性节点中的值
NSString *worktype=[dic
objectForKey:@"_worktype"];
object.worktype=worktype;
}
if ([dic objectForKey:@"_grouptype"]) {
//获取属性节点中的值
NSString *grouptype=[dic
objectForKey:@"_grouptype"];
object.grouptype=grouptype;
}
if ([dic objectForKey:@"_isonline"]) {
//获取属性节点中的值
NSString *isonline=[dic
objectForKey:@"_isonline"];
object.isonline=isonline;
}
[parserObjects
addObject:object];
}
}
failure:^(AFHTTPRequestOperation *operation,
NSError *error)
{
NSLog(@"请求失败=%@",error);
NSString *html = operation.responseString;
NSLog(@"请求失败返回的字符串~~~~~~~:%@",html);
}
];
[httpClient
enqueueHTTPRequestOperation:operation];
}
相关文章推荐
- 关于iOS 委托 代理 协议 的理解
- iOS 获取触摸,移动手势
- nagios插件ndoutile
- iOS开发判断手机号格式
- iOS 陀螺仪 加速计
- iOS高德定位和自带逆地理编码进行坐标转换
- 【有疑问,待解决】iOS 9 memory leak
- ios8 gps定位不好用
- zposition是啥东西?
- iOS开发多线程篇—多线程简介
- iOS 通知对应的方法多次执行问题
- [黑马IOS自学第十七篇]OC BLOCK学习
- iOS_24_画画板(含取色板)
- bios错误信息
- iOS 火星坐标系转换 扩展 源代码
- iOS项目
- 如何实现例如iOS的listview 的弹性效果
- IOS时间戳
- iOS 开发中遇见的小bug
- iOS精益求精 设计模式