这篇文章,我将通过一个简单的例子来展现iPhone通过get和post方式请求asp.net webservice。
webservice
1、创建一个webservice
2、在webconfig中启用http get 和http post。
<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的方式:
<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 = [
NSURL
URLWithString:
|
<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 = [
NSMutableURLRequest
requestWithURL:url];
|
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
NSString
*msgLength = [
NSString
stringWithFormat:@“%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 = [[
NSURLConnection
alloc] 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 = [[
NSMutableData
data] retain];
|
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
}
|
作者:
朱祁林
出处:
http://zhuqil.cnblogs.com