程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-Swift使用Alamofire調用C# Webservice的問題

ios-Swift使用Alamofire調用C# Webservice的問題

編輯:編程綜合問答
Swift使用Alamofire調用C# Webservice的問題

同事已經使用安卓調用成功,傳一個JSON給Webservice,並返回一個JSON
已經將webservice架設到外網服務器上了http://115.29.41.12:83/serForImccoy.asmx,大神幫忙調用一下看看,我寫的代碼如下,但一直報錯:
var wcfurlString = "http://115.29.41.12:83/serForImccoy.asmx/isUser"
Alamofire.request(Method.POST, wcfurlString, parameters: ["userId" : id , "pwd" : psw], encoding: ParameterEncoding.JSON).responseJSON { ( request, response, JSON, error) in
println(request)
println(response)
println(JSON)
println(error)
}
}
輸出信息如下:
{ URL: http://115.29.41.12:83/serForImccoy.asmx/isUser }
Optional( { URL: http://115.29.41.12:83/serForImccoy.asmx/isUser } { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 3297;
"Content-Type" = "text/html; charset=utf-8";
Date = "Tue, 18 Aug 2015 02:56:33 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} })
nil
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7f9cec9a2d20 {NSDebugDescription=Invalid value around character 0.})

最佳回答:


 func reqWebService() {
        let URL = NSURL(string: "http://115.29.41.12:83/serForImccoy.asmx")!
        let action: String = "http://imccoy.com/webservices/isUser"
        let mutableURLRequest: NSMutableURLRequest = NSMutableURLRequest(URL:URL)

        let values = "{\"userId\": \"test\", \"pwd\": \"pwd\"}";
        let soapMsg: String = toSoapMessage("isUser", params: "json", paramValues: values)

        //mutableURLRequest.setValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        //麼的,oc中,Content-Type是application/soap+xml就可以訪問,swift裡,就不行,必須與.asmx接口保持一致!
        mutableURLRequest.setValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        mutableURLRequest.setValue(action, forHTTPHeaderField: "SOAPAction")
        mutableURLRequest.setValue(String(soapMsg.length), forHTTPHeaderField: "Content-Length")
        mutableURLRequest.HTTPMethod = "POST"
        mutableURLRequest.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding)

        var result = ""
        Alamofire.request(mutableURLRequest).responseString(encoding: NSUTF8StringEncoding) { (_, _, result: String?, _) -> Void in
            println("服務器返回的內容:\(result!)")
        }
    }

    func toSoapMessage(methodName: String, params: String, paramValues: String) -> String {
        var message: String = String()
        message += "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
        message += "<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/\">"
        message += "<soap:Body>"
        message += "<\(methodName) xmlns=\"http://imccoy.com/webservices/\">"
        message += "<\(params)>\(paramValues)</\(params)>"
        message += "</\(methodName)>"
        message += "</soap:Body>"
        message += "</soap:Envelope>"
        println("請求消息體: \(message)")
        return message
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved