程序咪 2015-08-18 03:04 采纳率: 100%
浏览 2934
已采纳

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.})

  • 写回答

3条回答

  • dapan728 2015-08-20 17:07
    关注
     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
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)