Estou com um problema que passo a explicar, penso que achei uma maneira para chamar os métodos que estão no servidor, no entanto quando chamo um método que tem parâmetros, obtenho um erro, e quando chamo um método que não tem parâmetros corre tudo bem. Alguém me consegue explicar o porque disto acontecer ou o que estou a fazer mal?
Código de chamada do serviço
statusCode should be 200, but is 500
resposta do servidor ******** = Optional(<NSHTTPURLResponse: 0x7fc90a626350> { URL: https://*****.*****.com/****.slet request = NSMutableURLRequest(URL: NSURL(string: "https://*****.*****.com/****.svc/rest/ListRules")!)
request.HTTPMethod = "POST"
let postString = "ID=10"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("resposta do servidor ******** = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString ++++++++++++ = \(responseString)")
}
task.resume()
O erro que obtenho é o seguinte:
vc/rest/ListRules} { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 1937;
"Content-Type" = "application/xml; charset=utf-8";
Date = "Wed, 27 Jan 2016 10:39:58 GMT";
Server = "Microsoft-IIS/8.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} })
responseString ++++++++++++ = Optional(<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code>
<Value>Receiver</Value><Subcode><Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</Value></Subcode></Code>
<Reason><Text xml:lang="en-US">The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.
This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.</Text></Reason><Detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/>
<Message>The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding.
See the documentation of WebContentTypeMapper for more details.</Message><StackTrace> at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.InvalidOperationException</Type></ExceptionDetail></Detail></Fault>)
Código que me vai resolver o problema no entanto estou a ter um erro ao passar o parâmetro da função
let request = NSMutableURLRequest(URL: NSURL(string: "https://*******.*******.com/******.***/***/METODO")!) request.HTTPMethod = "POST"
var postString:NSString = "ID=23" //--> ERRO?
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
e o erro é "Encountered unexpect charater 'I'" e este i é o do parametro ID, alguem me consegue ajudar?
guard let data = data where error == nil else { return }
NSASCIIStringEncoding