您的位置:首页 > 移动开发 > IOS开发

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];

    

    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: