iphone之通过get和post方式请求asp.net webservice


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

webservice

1、创建一个webservice

iphone之通过get和post方式请求asp.net<wbr>webservice

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

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> < webServices >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
< protocols >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
< add<wbr></wbr> name ="HttpSoap" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
< add<wbr></wbr> name ="HttpPost" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
< add<wbr></wbr> name ="HttpGet" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
< add<wbr></wbr> name ="Documentation" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</ protocols >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</ webServices >

iphone客户端调用:

1、get的方式:

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

2、post的方式:

NSString*postString =@"123";
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>NSURL*url = [NSURLURLWithString:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>@“http://10.5.23.117:5111/Service1.asmx/HelloWorld2”];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>NSMutableURLRequest*req = [NSMutableURLRequestrequestWithURL:url];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>NSString*msgLength = [NSStringstringWithFormat:@“%d”, [postString length]];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>[req addValue:@“application/x-www-form-urlencoded”
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>forHTTPHeaderField:@“Content-Type”];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>[req addValue:msgLength forHTTPHeaderField:@“Content-Length”];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>[req setHTTPMethod:@“POST”];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>[req setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>[activityIndicator startAnimating];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>conn = [[NSURLConnectionalloc] initWithRequest:req delegate:self];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>if(conn) {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>webData = [[NSMutableDatadata] retain];
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>}

你可能感兴趣的:(webservice)