iphone http GET POST 调用webservice

前提是启用web.config文件下的post 和get访问方法,默认是soap访问

<webServices>

<protocols>

<add name="HttpSoap"/>

<add name="HttpPost"/>

<add name="HttpGet"/>

<add name="Documentation"/>

</protocols>

</webServices>

 

 

1,Using HTTP GET

 

请求格式

GET /iptocountry.asmx/FindCountryAsXml?V4IPAddress=string HTTP/1.1 
Host: www.ecubicle.net 
HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length

 

 (1)请求的地址为http://www.ecubicle.net/iptocountry.asmx/FindCountryAsXml?V4IPAddress=“aa”.

          发送的数据格式必须为键值对如key1=value1&key2=value2&key3=value3.

(2) 请求的Content-Type 格式为“text/xml; charset=utf-8.”

(3)请求的内容长度(Content-Length)为 0

(4)请求的方式为GET.

(5)返回结果发送为

<?xml version=”1.0”?> 
xml result

 

发送同步请求 

 - (IBAction)buttonClicked:(id)sender {
NSString *queryString =[NSString stringWithFormat:@“http://www.ecubicle.net/iptocountry.asmx/FindCountryAsXml?V4IPAddress=%@“,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];
}
}
 
 

2: Using HTTP POST

 

请求格式
POST /iptocountry.asmx/FindCountryAsXml HTTP/1.1 
Host: www.ecubicle.net 
Content-Type: application/x-www-form-urlencoded 
Content-Length: length 
V4IPAddress=string

 (1)请求地址为http://www.ecubicle.net/iptocountry.asmx/FindCountryAsXml

 (2)Content-Type为“application/x-www-form-urlencoded”

 (3)Content-Length 为V4IPAddress=string的长度。

     The data to be sent is formatted as key/value pairs — key1=value1&key2=value2&key3=value3. Unlike
     HTTP GET, the data are not sent through the query string; it is sent after the HTTP headers.

(4)The HTTP Method is POST.

(5)返回结果格式为

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
<?xml version=”1.0”?> 
xml result

 

请求发送

 

- (IBAction)buttonClicked:(id)sender {
NSString *postString =[NSString stringWithFormat:@“V4IPAddress=%@“, ipAddress.text];

NSLog(postString);

NSURL *url = [NSURL URLWithString:@“http://www.ecubicle.net/iptocountry.asmx/FindCountryAsXml”];

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:NSUTF8StringEncoding]];

[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
}

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值