Soap访问Webservice

1>这东西挺恶心的大体上是拼接请求头,根据xml的要求拼接所有参数

POST /App.asmx HTTP/1.1
Host: 192.168.70.100
Content-Type: text/xml; charset=utf-8
Content-Length: lengthSOAPAction: "http://www.cnpc.com.cn/GetLocation"



  
    
      string
      string
    
  
  
    
      string
    
  


代码实现

NSString *soapMessage = [NSString stringWithFormat:@"\
                             \
                             \
                             \
                             string\
                             string\
                             \
                             \
                             \
                             \
                             %@\
                             \
                             \
                             ",studentUid];
    
    NSURL *Url = [NSURL URLWithString:@"http://192.168.70.100:8999/App.asmx"];
    NSMutableURLRequest *finalRequest = [NSMutableURLRequest requestWithURL:Url];
    
    NSData *soapdata = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
    NSString *messageLength=[NSString stringWithFormat:@"%ld",[soapMessage length]];
    
    [finalRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [finalRequest addValue:@"http://www.cnpc.com.cn/GetLocation" forHTTPHeaderField:@"SOAPAction"];
    [finalRequest addValue:@"192.168.70.100" forHTTPHeaderField:@"HOST"];
    [finalRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
    [finalRequest setHTTPMethod:@"POST"];
    [finalRequest setHTTPBody:soapdata];
    
    
    [[[NSURLSession sharedSession] dataTaskWithRequest:finalRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            NSLog(@"连接错误 %@",error);
            return;
        }

你可能感兴趣的:(Soap访问Webservice)