错误域=NSPOSIXErrorDomain 代码=100“协议错误"; [英] Error Domain=NSPOSIXErrorDomain Code=100 "Protocol error"

查看:43
本文介绍了错误域=NSPOSIXErrorDomain 代码=100“协议错误";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .get 方法使用 Alamofire 调用服务.该请求没有什么特别的,没有参数,只有一个 Authorization 标头.

I am trying to call a service using Alamofire using a .get method. The request has nothing special no parameters just an Authorization header.

我在这里有点生气,因为当我使用相同的 URL 和授权令牌在邮递员上运行它时,请求工作正常,但是当我使用 Alamofire 运行我的应用程序代码时,它返回此错误:

I am going a bit mad here because to request works fine when I run it on postman with the same URL and Authorization token, but when I run my app code with Alamofire it returns this error:

Error Domain=NSPOSIXErrorDomain Code=100 "Protocol error" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bbd83ad0b10000000000000000}, _kCFStreamErrorCodeKey=100},StreamErrorCodeKey=101},Error 2

Error Domain=NSPOSIXErrorDomain Code=100 "Protocol error" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bbd83ad0b10000000000000000}, _kCFStreamErrorCodeKey=100, _kCFStreamErrorDomainKey=1}

我正在使用:

Alamofire.request("https://myserverURL", method: .get, parameters: [:], encoding: JSONEncoding.default, headers: ["Authorization":"myToken"])
        .responseJSON {response in 

    guard response.result.error == nil else { 
       //HERE IS WHERE IS GOING IN WITH THE ERROR
    }
}

任何想法将不胜感激或指向正确的方向:)

Any thoughts will be much appreciated or point me in the right direction :)

推荐答案

我在使用基础类 URLSession 的 Cocoa 中遇到了与您完全相同的错误.经过几个小时的调试,问题出在 HTTP 请求正文中.

I got exact same error as yours in Cocoa with foundation class URLSession. After hours debugging the issue lies in the HTTP request body.

您真的应该尝试转储 HTTP 请求/响应正文,看看是否有一些格式错误的字段.例如,Content-LengthContent-Type 是对还是错?根据我的经验,如果这些必需的(基本)标头格式错误,它可能无法工作,具体取决于您的操作系统或其他中间网络接受(例如代理、网关、服务器等)

You should really try to dump the HTTP request/response body to see if there are some malformed fields. For example, Content-Length and Content-Type are right or missing? In my experience if these required(fundamental) headers are malformed, it may not work depending on your OS or other intermediate network accept(e.g. proxy, gateway, server, etc.)

我的错是在方法 URLRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 中错放了函数参数,结果是错误的Content-Type HTTP 字段.

My fault is misplacing function params in the method URLRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type"), which ends up with a wrong Content-Type HTTP field.

但是,它适用于 macOS 10.12 但不适用于 12.11,所以您应该确保您的 HTTP 请求正文没有格式错误.

However, it works in macOS 10.12 but not 12.11 so you should makes sure your HTTP Request body is not malformed.

希望有帮助.

形成您的示例代码,我猜 encoding: JSONEncoding.default 是错误的.由于HTTP GET 方法没有主体,严格/不健壮的网络组件会拒绝/不理解它.

Form your sample code, I guess the encoding: JSONEncoding.default is wrong. Since an HTTP GET method has NO body, a strict/not-robust network component would reject/not understand it.

您的目标是在请求标头中设置 Accept: application/json,但是如果您确定响应正文类型,则不需要.

What your goal is set the Accept: application/json in the request header, however it's not required if you're sure the response body type.

这篇关于错误域=NSPOSIXErrorDomain 代码=100“协议错误";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆