描述:
修改webservice的Web.Config
在 <system.web> .... </system.web>區段中加入下面這一段
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
这样在可以用 异步请求1的方法,否则只能用异步请求2的方法,加上一大段繁冗的soapMsg 包头代码
但是加了上面的红褐色代码webService就会不安全,可以进行远程测试
请教下有无其他方法可以不用加soapMsg 代码
复制代码
func AsynchronousRequest3() //异步请求1
{
var req = NSMutableURLRequest(URL: NSURL(string: "http://172.18.1.105/WS/WSForTest.asmx/Sum")!)
req.HTTPMethod = "POST"
req.timeoutInterval=60
req.HTTPBody=NSString(string: "a=1&&b=2").dataUsingEncoding(NSUTF8StringEncoding)
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (resp, data, error) -> Voidin
iflet e = error {
println(e)
}
iflet d = data {
println(NSString(data: d , encoding: NSUTF8StringEncoding))
}
}
}
func AsynchronousRequest4() //异步请求2
{
var url8: NSURL = NSURL(string: "http://172.18.1.105/WS/WSForTest.asmx")!
var soapMsg = "<?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><Sum xmlns='http://tempuri.org/' ><a>1</a><b>2</b></Sum></soap:Body></soap:Envelope>"
var request = NSMutableURLRequest(URL: url8)
var msgLength = String(count(soapMsg))
request.HTTPMethod = "POST"
request.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.addValue("http://172.18.1.105/WS/WSForTest.asmx/Sum", forHTTPHeaderField: "Action")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (resp, data, error) -> Voidin
println(resp)
iflet e = error {
println(e)
}
iflet d = data {
println(NSString(data: d , encoding: NSUTF8StringEncoding))
}
}
}