Swift 3 - Http-сообщение для веб-службы Https wcf

В моем консольном приложении xcode (10.12) я использую HTTP post-запросы для отправки данных в веб-службу wcf. Если этот веб-сервис работает с http, проблем нет. Но если я перехожу на безопасное (https) соединение, то я не могу связаться с веб-сервисом. Я получаю код ошибки 404.

class request :NSObject,URLSessionDelegate{


func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}

func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
    if let err = error {
        print("Error: \(err.localizedDescription)")
    } else {
        print("Error. Giving up")
    }
}


func makeHTTPPostRequest(path: String, params: Dictionary<String,AnyObject>) {
    let request = NSMutableURLRequest(url: NSURL(string: path)! as URL)
    request.httpMethod = "POST"


    do  {
        request.httpBody = try JSONSerialization.data(withJSONObject: params, options: JSONSerialization.WritingOptions.prettyPrinted)
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 10
        configuration.timeoutIntervalForResource = 10

        let session = URLSession(configuration: configuration,delegate: self, delegateQueue: nil)


        let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in

            print(response)

        })
        task.resume()

    }
    catch{

    }

}

И код веб-сервиса примерно такой:

     [WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    string Test(string name);

Я вызываю метод:

var params = Dictionary<String,AnyObject>()

params ["name"] = "Привет" как AnyObject?

var r = request () r.makeHTTPPostRequest (путь: " https://192.168.1.104/sample/Service1.svc/Test", params: params)

Это ответ от сервера:

{ status code: 404, headers {
"Cache-Control" = private;
"Content-Length" = 0;
Date = "Sat, 25 Mar 2017 14:59:29 GMT";
Server = "Microsoft-IIS/7.5";
"Set-Cookie" = "ASP.NET_SessionId=l2omo5wflafesmcw2j23sw4t; path=/; HttpOnly";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";} 

Так что я делаю не так? Есть ли у вас какие-либо идеи?

Заранее спасибо!

1 ответ

Я нашел свою ошибку, неправильная конфигурация web.config. Код выше работает нормально.

Лучший

Другие вопросы по тегам