首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift 3- Http post to Https wcf web服务

Swift 3- Http post to Https wcf web服务
EN

Stack Overflow用户
提问于 2017-03-25 23:06:17
回答 1查看 726关注 0票数 0

在我的xcode控制台应用程序(10.12)中,我使用http post请求将数据发送到wcf web服务。如果该web服务与http一起工作,则不会有任何问题。但是,如果我更改为安全(https)连接,那么我将无法与web服务通信。我得到了404错误代码。

代码语言:javascript
复制
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{

    }

}

和web服务代码类似的东西:

代码语言:javascript
复制
     [WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    string Test(string name);

我正在调用方法:

代码语言:javascript
复制
var params = Dictionary<String,AnyObject>()

参数“name”= "Hi“as AnyObject?

var r=请求()参数(r.makeHTTPPostRequest:“https://192.168.1.104/sample/Service1.svc/Test”,params:params)

这是来自服务器的响应:

代码语言:javascript
复制
{ 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";} 

那么我做错了什么呢?你有什么想法吗?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-03-29 23:53:59

我发现了我的错误,错误的web.config配置。上面的代码运行良好。

最好的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43017970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档