ios請求webservice幾種方式ios
(1)回傳soap字符串進行請求web
(2)http://xxx/方法名/參數名=參數值 ===>這種方式請求spring
基於不少新手都對wsdl都不認識,又談何請求,更別提soap字符串了,我在這裏主要圖解說明如何拼湊soap字符串進行請求ide
一.命名空間在根目錄上url
(1)soap字符串格式以下: (注:只有文字部分纔要修改,非文字部份都是固定的)spa
<?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/" xmlns:前綴名(如:ns1)="webservice命名空間">
<soap:Header>code<!----頭內容----->
</soap:Header>
<soap:Body>
<前綴名:方法名>
<參數名1>參數值1</參數名1>
<參數名2>參數值2</參數名2>
.......
</前綴名:方法名>
</soap:Body>
</soap:Envelope>orm
a.soap:Header有請求頭就加上,沒上就可去悼soap:Header節點
b.若是調用方法沒有參數,則改爲<前綴名:方法名></前綴名:方法名>
(2)以http://policy.mofcom.gov.cn/webservice/ClawWS?wsdl 作爲參考圖解說明
a.以咱們要調用simpleQuery方法爲例,則soap字符串爲:
<?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/" xmlns:ns1="http://webservice.modules.springside.org">
<soap:Header>
<authHeader>
<userId>wanfang</userId>
<userPass>wanfang</userPass>
</authHeader>
</soap:Header>
<soap:Body>
<ns1:simpleQuery>
<queryString>法律</queryString>
<section>all</section>
<pageNo>1</pageNo>
</ns1:simpleQuery>
</soap:Body>
</soap:Envelope>xml接口
b.圖解說明
二.命名空間在方法名上
(1)soap字符串格式以下: (注:只有文字部分纔要修改,非文字部份都是固定的)<?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:Header>
<!----頭內容----->
</soap:Header> <soap:Body> <方法名 xmlns="webservice命名空間"> <參數1>參數值1</參數1> <參數2>參數值2</參數2>..... </方法名>
</soap:Body></soap:Envelope>
a.soap:Header有請求頭就加上,沒上就可節點b.若是調用方法沒有參數,則改爲<方法名 xmlns="webservice命名空間"></方法名>(2)以http://webservice.webxml.com.cn/WebServices/StockInfoWS.asmx?wsdl 進行圖解說明
a.咱們要調用getStockInfo方法,則soap字符串爲:
<?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>
<getStockInfo xmlns="http://webxml.com.cn/">
<theStockCode>參數值1</theStockCode>
<userID>參數值2</userID>
</getStockInfo>
</soap:Body>
</soap:Envelope>
b.圖解說明
三.ios調用webservice
NSString *soapMsg=@"咱們拼成的soap字符串";
NSString *space=@"webservice命名空間";
NSString *methodname=@"要調用的方法名";
NSURL *url=[NSURL URLWithString:@"webservice地址"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
NSString *soapAction=[NSString stringWithFormat:@"%@%@",space,methodname];
//頭部設置
NSDictionary *headField=[NSDictionary dictionaryWithObjectsAndKeys:[url host],@"Host",
@"text/xml; charset=utf-8",@"Content-Type",
msgLength,@"Content-Length",
soapAction,@"SOAPAction",nil];
[request setAllHTTPHeaderFields:headField];
//超時設置
[request setTimeoutInterval: 30 ];
//訪問方式
[request setHTTPMethod:@"POST"];
//body內容
[request setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
注:
a.有些webservice調用須要設SOAPAction,有些則不要,要就加上,不要就去悼
b.若是用第一種[命名空間在根目錄上]方式生成的soap字符串調用會有錯,則用第二種[命名空間在方法名上]生成的字符串調用
c.仍是不行,請教寫webservice接口的人員,找不到人,只能自已琢磨了