iPhone访问.Net WebService的3种方法

本文介绍通过三种不同方式调用 C# WebService 的方法:使用 HTTPPost 发送表单数据、使用 HTTPGet 传递参数以及构建 SOAP 消息进行请求。示例代码展示了 Objective-C 中具体的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# 代码

public class WebService : System.Web.Services.WebService
{
	[WebMethod(EnableSession = true)]
        public bool Login(string email, string password)
        {
            return true;
        }
}

1.Http Post

    NSString *postString = [NSString stringWithFormat:@"email=%@&password=%@",inEmail, inPassword];
    NSString *urlString = @"http://localhost/Webservice.asmx/Login";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];     
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];  
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];     
    [theRequest setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];                               
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;
    urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
    NSString *retStr = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",retStr);

2.Http Get

    NSString *urlString = @"http://localhost/Webservice.asmx/Login?password=123&email=222";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];  
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;
    urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
    NSString *retStr = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",retStr);

3.SOAP

    NSString *soapString = [NSString stringWithFormat:
                            @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                            "<soap:Envelope\n"
                            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
                            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                            "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
                            "<soap:Body>\n"
                            "<Login>\n"
                            "<email>%@</email>\n"
                            "<password>%@</password>\n"
                            "</Login>\n"
                            "</soap:Body>\n"
                            "</soap:Envelope>", inEmail,inPassword];

    NSURL *url = [NSURL URLWithString:@"http://localhost/WebService.asmx"];  
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapString length]];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];     
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
    [theRequest addValue: @"/Login" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];     
    [theRequest setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];
    NSData* urlData;
    NSURLResponse *response;
    NSError *error = nil;
    urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
    NSString* retStr = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",retStr);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值