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"

<?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:Header>
    <RequestHeader xmlns="http://www.cnpc.com.cn/">
      <AuthUsername>string</AuthUsername>
      <AuthPassword>string</AuthPassword>
    </RequestHeader>
  </soap:Header>
  <soap:Body>
    <GetLocation xmlns="http://www.cnpc.com.cn/">
      <studentUid>string</studentUid>
    </GetLocation>
  </soap:Body>
</soap:Envelope>

代码实现

NSString *soapMessage = [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:Header>\
                             <RequestHeader xmlns=\"http://www.cnpc.com.cn/\">\
                             <AuthUsername>string</AuthUsername>\
                             <AuthPassword>string</AuthPassword>\
                             </RequestHeader>\
                             </soap:Header>\
                             <soap:Body>\
                             <GetLocation xmlns=\"http://www.cnpc.com.cn/\">\
                             <studentUid>%@</studentUid>\
                             </GetLocation>\
                             </soap:Body>\
                             </soap:Envelope>",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;
        }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android本身是不提供soap访问webservice的功能,引用了ksoap2-android的jar实现了...
    小马哥亿天洋阅读 512评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,393评论 25 708
  • 接昨天的话题。 第一步:要认识情绪,孩子哭了,不要着急安慰,先分析下他是为什么哭。通常在分析情绪的过程中,随着理性...
    船长辛巴达阅读 558评论 0 2
  • 1 . 他开始说一些接近崩灭的无序呓语 常常他觉得排解无能或是自我惩戒的借口会绽放伤疤 然后蔓延怒茁 顺着血肉蜿蜒...
    冗辰阅读 252评论 0 1