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

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

问题描述

我正在尝试使用.get方法使用Alamofire调用服务。
请求没有任何特殊的参数只是一个授权标题。

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

我在这里有点生气,因为我在postman上运行它时请求工作正常相同的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:


错误域= NSPOSIXErrorDomain代码= 100 协议错误UserInfo = {NSErrorPeerAddressKey = {length = 16,capacity = 16,bytes = 0x100201bbd83ad0b10000000000000000},_ kCFStreamErrorCodeKey = 100,_kCFStreamErrorDomainKey = 1}

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

任何想法都会很有用eciated或指向我正确的方向:)

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

推荐答案

我在基础类<$ c的Cocoa中得到了与你相同的错误$ C> URLSession 。经过几个小时的调试,问题在于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-Length Content-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.

希望它有所帮助。

形成示例代码,我想编码: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天全站免登陆