IOS developmentUnderstanding iPhoneGetAndPostThis article uses a simple example to show how the iPhone usesGetAndPostRequest asp.net webservice.
Webservice
1. Create a webservice,
2. enable http get and http post in webconfig.
- <webServices>
- <protocols>
- <add name="HttpSoap"/>
- <add name="HttpPost"/>
- <add name="HttpGet"/>
- <add name="Documentation"/>
- </protocols>
- </webServices>
Iphone client call:
1. get method:
- 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 method:
- 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];
- }
Summary:IOS developmentUnderstanding iPhoneGetAndPostThis article is helpful to you!