前提是启用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
请求格式
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 result
发送同步请求
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
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)返回结果格式为
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];
}
}