iOS开发之了解iPhone中Get和Post方式

移动开发 iOS
本文主要是通过iPhone中Get和Post方式去请求asp.net webservice的一个实例,让我们来学习GET和POST方式的用法,先来看详细内容。

iOS开发之了解iPhoneGetPost方式是本文要介绍的内容,本篇文章通过一个简单的例子来展现iPhone通过getpost方式请求asp.net webservice。

webservice

1、创建一个webservice,如图:

iOS开发之了解iPhone中Get和Post方式

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

<webServices> 
    <protocols> 
        <add name="HttpSoap"/> 
        <add name="HttpPost"/> 
        <add name="HttpGet"/> 
        <add name="Documentation"/> 
    </protocols> 
</webServices> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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];  
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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];    
    }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

小结:iOS开发之了解iPhoneGetPost方式的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2009-09-28 13:23:54

HTTP编程

2011-08-04 17:19:49

iPhone开发 Xcode 文档

2013-07-23 07:34:54

iOS开发学习适配iphone5

2011-08-01 18:44:16

iPhone开发 UIALertVie UIActionSh

2011-08-22 15:15:49

iPhone开发NSMutableAr排序

2011-06-08 11:02:14

GetPost

2009-07-29 16:53:52

ASP.NET Get

2011-08-08 10:23:41

iPhone 流播放 文件

2011-06-08 11:05:38

getpost

2023-08-07 09:25:06

GETPOSTHTTP请求

2011-07-08 14:58:16

iPhone Xcode iOS

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-01 18:27:58

iPhone开发 UISearchBa

2014-07-24 10:00:21

2011-08-12 13:30:27

iPhoneASIFormData架构

2013-07-22 16:10:35

iOS游戏设计营销

2013-07-29 05:01:31

iOS开发iOS开发学习按钮拖动和点击

2018-01-16 15:06:36

iPhone XiOS

2011-07-29 15:47:21

iPhone开发 Objective- C

2011-07-08 17:45:19

iPhone 文档
点赞
收藏

51CTO技术栈公众号