IPhone developed in IOS requests Asp.net WebService through get and post

Source: Internet
Author: User

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];} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.