读书人

iphone 对Web Services的三种请求模式

发布时间: 2012-06-28 15:20:03 作者: rapoo

iphone 对Web Services的三种请求方式soap get post

一:Using SO AP 1.1

?

?POST /iptocountry.asmx HTTP/1.1
Host: www.ecubicle.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: “http://www.ecubicle.net/webservices/FindCountryAsXml”
<?xml version=”1.0” encoding=”utf-8”?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=http://www.w3.org/2001/XMLSchema
xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/“>
<soap:Body>
<FindCountryAsXml xmlns=”http://www.ecubicle.net/webservices/“>
<V4IPAddress>string</V4IPAddress>
</FindCountryAsXml>
</soap:Body>
</soap:Envelope>

?

(1)请求的webservice url的地址为“http://www.ecubicle.net/iptocountry.asmx”

(2)url 的 SOAPAction?是 “http://www.ecubicle.net/webservices/FindCountryAsXml”

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

(4)请求的方式为post

(5)请求的内容(SOAP request)格式为

?<?xml version=”1.0” encoding=”utf-8”?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/“>
<soap:Body>
<FindCountryAsXml xmlns=”http://www.ecubicle.net/webservices/“>
<V4IPAddress>string</V4IPAddress>
</FindCountryAsXml>
</soap:Body>
</soap:Envelope>

?(6)请的内容长度(Content-Length)为SOAP request的字节长度

(7)返回的结果格式为

?HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version=”1.0” encoding=”utf-8”?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/“>
<soap:Body>
<FindCountryAsXmlResponse xmlns=”http://www.ecubicle.net/webservices/“>
<FindCountryAsXmlResult>xml result</FindCountryAsXmlResult>
</FindCountryAsXmlResponse>
</soap:Body>
</soap:Envelope>

?

?- (IBAction)buttonClicked:(id)sender {

?

NSString *soapMsg =[NSString stringWithFormat:@“<?xml version=\“1.0\“ encoding=\“utf-8\“?>”
“<soap:Envelope xmlns:xsi=”“\“http://www.w3.org/2001/XMLSchema-instance\“ “
“xmlns:xsd=\“http://www.w3.org/2001/XMLSchema\“ ““xmlns:soap=\“http://schemas.xmlsoap.org/soap/envelope/\“>”“<soap:Body>”
“<FindCountryAsXml xmlns=\“http://www.ecubicle.net/webservices/\“>”
“<V4IPAddress>%@</V4IPAddress>”“</FindCountryAsXml>”“</soap:Body>”“</soap:Envelope>”, ipAddress.text];


//---print it to the Debugger Console for verification---
NSLog(soapMsg);


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


NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];


//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@“%d”, [soapMsg length]];


[req addValue:@“text/xml; charset=utf-8” forHTTPHeaderField:@“Content-Type”];


[req addValue:@“http://www.ecubicle.net/webservices/FindCountryAsXml”forHTTPHeaderField:@“SOAPAction”];
[req addValue:msgLength forHTTPHeaderField:@“Content-Length”];


//---set the HTTP method and body---
[req setHTTPMethod:@“POST”];


[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];


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

?

?

二:Using SO AP 1.2

?

请求格式

?POST /iptocountry.asmx HTTP/1.1
Host: www.ecubicle.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version=”1.0” encoding=”utf-8”?>
<soap12:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:soap12=”http://www.w3.org/2003/05/soap-envelope”>
<soap12:Body>
<FindCountryAsXml xmlns=”http://www.ecubicle.net/webservices/“>
<V4IPAddress>string</V4IPAddress>
</FindCountryAsXml>
</soap12:Body>
</soap12:Envelope>

?

返回结果格式

?HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version=”1.0” encoding=”utf-8”?>
<soap12:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:soap12=”http://www.w3.org/2003/05/soap-envelope”>
<soap12:Body>
<FindCountryAsXmlResponse xmlns=”http://www.ecubicle.net/webservices/“>
<FindCountryAsXmlResult>xml result</FindCountryAsXmlResult>
</FindCountryAsXmlResponse>
</soap12:Body>
</soap12:Envelope>

?

?

请求代码同SOAP 1.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];
}
}??

四:?Using HTTP POS T

?

请求格式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];
}
}

?

读书人网 >操作系统

热点推荐