oc调用web service接口

方法一:直接将参数用&拼接在调用的接口之后,然后Request请求即可。

NSString * putSFC(const char * url,int timout)

{
    NSURL * strURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
    if (!strURL)
    {
        NSLog(@"Invalid URL : %@",[NSString stringWithUTF8String:url]);
        return @"Invalid URL";
    }

    NSURLRequest* request = [NSURLRequest requestWithURL:strURL cachePolicy:(NSURLRequestCachePolicy)0 timeoutInterval:timout];
    NSURLResponse* response=nil;
    NSError* error=nil;
    NSData* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (data==nil) {
        sprintf(tmper, "-----request=%s;data=%s",[[NSString stringWithFormat:@"%@",request] UTF8String],[[NSString stringWithFormat:@"%@",data] UTF8String]);
        SaveDebug(tmper,__func__,__LINE__);
        return @"empty data";
    }
    sprintf(tmper, "-----request=%s;data=%s",[[NSString stringWithFormat:@"%@",request] UTF8String],[[NSString stringWithFormat:@"%@",data] UTF8String]);
    SaveDebug(tmper,__func__,__LINE__);

    NSString * str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    if (str==nil) {
        return @"empty str";
    }
    NSLog(@"SFC Response : %@",str);
    return str;//[str UTF8String];

}


int SF_Login()
{
    NSString *ret;
    NSString *url = @"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx/UserLogin";
    NSString *username = [CTestContext::m_dicGlobal objectForKey:@"SF_Username"];
    NSString *password = [CTestContext::m_dicGlobal objectForKey:@"SF_Password"];
    const char *msg = [[NSString stringWithFormat:@"%@?User=%@&Password=%@",url,username,password] UTF8String];
    sprintf(tmper, "-----msg=%s",msg);
    SaveDebug(tmper,__func__,__LINE__);
    ret = putSFC(msg, 5);
    if ([ret  isEqual: @"OK"]) {
        return 0;
    }
    else{
        return -1;
    }
}


方法二:将参数拼写成xml格式然后发送。

            second method 学习至http://my.oschina.net/plumsoft/blog/75277。

    NSString *webServiceBodyStr = [NSString stringWithFormat:
                                   @"<UserLogin xmlns=\"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx/\">"
                                   "<User>%@</User>"
                                   "<Password>%@</Password>"
                                   "</UserLogin>",username,password];//这里是参数
    ret = putxml(@"UserLogin", webServiceBodyStr, 5);


NSString *putxml(NSString *action,NSString* xml,int timeout)
{
    NSURL * strURL = [NSURL URLWithString:@"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx"];
    if (!strURL)
    {
        NSLog(@"Invalid URL : %s","http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx");
        return @"Invalid URL";
    }
    NSString *webServiceStr = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                               "<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/\">\n"
                               "<soap:Body>\n"
                               "%@\n"
                               "</soap:Body>\n"
                               "</soap:Envelope>",xml];
    NSString *SOAPActionStr = [NSString stringWithFormat:@"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx/%@",action];
    NSString *msgLength = [NSString stringWithFormat:@"%ld", xml.length];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:strURL];
    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"];
    [theRequest setHTTPMethod:@"POST"];
    
    [theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *con = [[NSURLConnection alloc]initWithRequest:theRequest delegate:nil];

//    NSURLSession *session = [NSURLSession sharedSession];
//    NSURLSessionDataTask *task = [session dataTaskWithRequest:theRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
//        NSLog(@"run into callback session , result = %@ , url = %@",result,response.URL);
//        if(error){
//            NSLog(@"session fail -> %@",error.localizedDescription);
//        }
//        
//    }];
//    [task resume];
    if (con) {
        msgData = [NSMutableData data];
        return @"OK";
    }
    else
    {
        return @"GG";
    }
}


-(void) connectionDidFinishLoading:(NSURLConnection *) connection 完成接受数据的时候在此函数内解析

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data 接受的数据添加到msgData内

{
    [msgData appendData:data];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值