This articleArticleI will use a simple example to show how iPhone requests Asp.net WebService through get and post.
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 > Copy code
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];
} Copy code
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: encoding]; [activityindicator startanimating]; Conn = [[nsurlconnection alloc] initwithrequest: req delegate: self]; If (conn) {webdata = [[nsmutabledata data] retain];}
In this article, I will use a simple example to show how iPhone requests Asp.net WebService through get and post methods.
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 > Copy code
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];
} Copy code
2. Post method:
nsstring * poststring = @ "123"; nsurl * url = [nsurl urlwithstring: @ "http: // 10.5.23.117: 5111/service1.asmx/helloworld2 "]; required * 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];}