iphone http GET POST 调用webservice

来源:互联网 发布:淘宝直播排名规则 编辑:程序博客网 时间:2024/06/05 16:34

前提是启用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];
}
}

 

 


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 半永久眉失败了怎么办 取环前2天同房了怎么办 怀孕了发现有子宫肌瘤怎么办 宫腔中央性粘连怎么办 孕妇宫腔粘连带怎么办 内膜厚怎么办吃什么好 功血引起的贫血怎么办 吃宫血宁后月经不来了怎么办 孕晚期胎心不好怎么办 胎心不好怎么办让住院 39 5胎心不好怎么办 肺长了一个肿瘤怎么办 血糖高伤口不愈合怎么办 有轻微的狐臭该怎么办 嘴上汗毛太重怎么办 风把裙子吹起怎么办 每天三四点醒来就睡不着怎么办 打游戏手汗太多怎么办 老年人胸闷气短呼吸困难怎么办 冒险岛宠物饿了怎么办 不小心喝了黑墨怎么办 压的双眼皮开了怎么办 割了双眼皮显老怎么办 心脏供血不足怎么办呢 怀孕了哮喘犯了怎么办 喝了电解质不拉怎么办 喝电解质散吐了怎么办 皮鞋买大了一号怎么办 皮鞋买小了挤脚怎么办 新买的皮鞋挤脚怎么办 熊类仓鼠变瘦怎么办 喜利得电锤锁了怎么办 请病假公司不批怎么办 淘宝扣满12分怎么办 吃了霉变的大米怎么办 魅族账号忘了怎么办 魅族账号忘记了怎么办 密保问题忘记了怎么办 yy忘记密保问题怎么办 qq忘记密保问题怎么办 魅族3固件损坏怎么办