为了账号安全,请及时绑定邮箱和手机立即绑定

iPhone请求asp.net webservice

标签:
iOS C#

这篇文章,我将通过一个简单的例子来展现iPhone通过get和post方式请求asp.net webservice。

webservice

1、创建一个webservice

2、在webconfig中启用http get 和http post。

复制代码        <webServices>
            <protocols>
                <add name="HttpSoap"/>
                <add name="HttpPost"/>
                <add name="HttpGet"/>
                <add name="Documentation"/>
            </protocols>
        </webServices>复制代码

iphone客户端调用:

1、get的方式:

复制代码NSString *queryString =
          [NSString stringWithFormat:
    @“http://10.5.23.117:5111/Service1.asmx/HelloWorld?param=123%@“,
    ipAddress.text];
    NSURL *url = [NSURL URLWithString:queryString];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    [req addValue:@“text/xml; charset=utf-8” forHTTPHeaderField:@“Content-Type”];
    [req addValue:0 forHTTPHeaderField:@“Content-Length”];
    [req setHTTPMethod:@“GET”];
    [activityIndicator startAnimating];
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) {
        webData = [[NSMutableData data] retain];
    }复制代码

2、post的方式:

?

NSString *postString =@"123";    NSURL *url = [NSURL URLWithString:        @“http://10.5.23.117:5111/Service1.asmx/HelloWorld2”];    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];    NSString *msgLength = [NSString stringWithFormat:@“%d”, [postString length]];    [req addValue:@“application/x-www-form-urlencoded”        forHTTPHeaderField:@“Content-Type”];    [req addValue:msgLength forHTTPHeaderField:@“Content-Length”];    [req setHTTPMethod:@“POST”];    [req setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];    [activityIndicator startAnimating];    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];    if (conn) {        webData = [[NSMutableData data] retain];    }


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消