当前位置:首 页 > 攻城湿 >Mac & iOS > 查看文章

Objective-C调用Web Service示例

Mac & iOS 你是第2304个围观者 0条评论 供稿者: 标签:, , ,

因为有的Web Service参数要求有顺序,所以参数用了NSArray

+ (NSData *) SoapCall:(NSString *)method  PostData:(NSString *)post
{
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    [postData autorelease];
     
    NSString *soapAction = [NSString stringWithFormat:@"%s/%@",SoapPrefix , method  ];
     
    NSURL *url=[[NSURL alloc]initWithString:[ [ NSString alloc ] initWithUTF8String: SoapURL ] ];
    NSMutableURLRequest  *request=[[NSMutableURLRequest alloc]init];
     
    [request setTimeoutInterval: 10 ];
    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    [request setURL: url ] ;
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
    [request setValue:soapAction forHTTPHeaderField:@"SOAPAction"];
     
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];  
     
     
    NSError *err=nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:request
                                       returningResponse:nil
                                                   error:&err];
    return data ;
}
 
 
+ (NSData *)SoapInvoke:(NSString *)method params:(NSArray *)params
{
    NSMutableString * post = [[ NSMutableString alloc ] init ] ;
    [ post autorelease ];
     
    [ post appendString:
     @"< ?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
     "<soap:envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
     " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
     " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
     "<soap:body>\n" ];
     
    [ post appendString:@"< "];
    [ post appendString:method];
    [ post appendString:@">\n"];
     
    for (NSDictionary *dict in params)
    {
        NSString *value = nil;
         
        NSString *key = [[dict keyEnumerator] nextObject];
         
        if (key != nil)
        {
            value = [dict valueForKey:key];
             
            [ post appendString:@"< "];
            [ post appendString:key];
            [ post appendString:@">"];
            if( value != nil )
            {
                [ post appendString:value];
            }
            else
            {
                [ post appendString:@""];
            }
             
            [ post appendString:@"\n"];
        }
    }
     
    [ post appendString:@"\n"];
     
    [ post appendString:
     @"</soap:body>\n"
     "</soap:envelope>\n"
     ];
     
    //    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"json"];
    //    NSString *test = [NSString stringWithContentsOfFile:filePath];
    NSLog(@"====================request=========================\n%@\n", post);
     
    return [self SoapCall:method PostData:post];
}
这家伙很懒,什么都没写!

—— zhaorong

zhaorong
你可能也喜欢Related Posts
众说纷纭Comments
大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头
小提示:直接粘贴图片到输入框试试
努力发送中...
  • 评论最多
  • 最新评论
  • 随机文章
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容
Copyright © zhaorong All Rights Reserved. 滇ICP备15006105号-1