2、在webconfig中启用httpget和httppost。" />

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

系统 2063 0

这篇文章,我将通过一个简单的例子来展现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 = [ 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> }

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论