【问题标题】:Retrofit response code 405 with message "method not allowed here"改造响应代码 405 并带有消息“此处不允许方法”
【发布时间】:2017-03-31 00:14:24
【问题描述】:

我正在使用改造,我有一个发布请求

interface NetworkApi {

@Headers("Content-Type: application/json")
@POST("/")
fun startLoginRequest(@Body loginModel : LoginModel) : Call<BaseResponseModel>

class Factory {
    var retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    companion object{
        fun getApi (): NetworkApi {
            val serverApi = NetworkApi.Factory()
            val api = serverApi.retrofit.create(NetworkApi::class.java)
            return api
        }
    }
}

}

当我使用此方法和响应方法时,调用响应正文始终为空。

fun startLoginRequest(){
    val api = NetworkApi.Factory.getApi()
    val request = api.startLoginRequest(loginRequestModel)

    request.enqueue(object : Callback<BaseResponseModel>{
        override fun onFailure(call: Call<BaseResponseModel>?, t: Throwable?) {

        }

        override fun onResponse(call: Call<BaseResponseModel>?, response: Response<BaseResponseModel>?) {
            //here response.body is null
        }
    })
}

奇怪的是,如果我使用 Postman 发送相同的对象,一切正常

这是 httpClient 拦截器日志

--> POST http://myExampleHost.net/ http/1.1
Content-Type: application/json

Content-Length: 95
Authorization: auth-value
--> END POST (95-byte body)
<-- 405 Method Not Allowed http://myExampleHost.net/ (198ms)
Allow: GET, HEAD, OPTIONS, TRACE, COPY, PROPFIND, LOCK, UNLOCK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 17 Nov 2016 08:04:57 GMT
Content-Length: 1107
<HTML>
<HEAD>
<TITLE>405 Method Not Allowed</TITLE>
<BASE href="/error_docs/"><!--[if lte IE 6]></BASE><![endif]-->
</HEAD>
<BODY>
<H1>Method Not Allowed</H1>
The HTTP verb used to access this page is not allowed.<P>
<HR>
<ADDRESS>
Web Server at example address
</ADDRESS>
</BODY>
</HTML>

现在我正在使用 OkHttp RequestBuilder 并且一切正常,我没有意识到我在上面的示例中缺少什么

val client = OkHttpClient()
    val JSON = MediaType.parse("application/json; charset=utf-8")
    val body = RequestBody.create(JSON,json)
    val request1 = Request.Builder()
            .url("http:example.com")
            .addHeader("content-type", "application/json")
            .method("POST", body)
            .build()

    client.newCall(request1).enqueue(object : okhttp3.Callback{
        override fun onFailure(call: okhttp3.Call?, e: IOException?) {

        }

        override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
            response?.body()?.string()?.print("RETROFIT response")
        }
    })

【问题讨论】:

    标签: android http kotlin retrofit2


    【解决方案1】:

    我最好的猜测是你试图发布到错误的 URL www.example.com/login// 而不是 www.example.com/login/。 API 对帖子可能有点挑剔。

    您可以添加日志拦截器,这样您就可以看到您发布的内容以及发布到哪个 URL,从而更容易调试。要进行设置,您需要将 compile "com.squareup.okhttp3:logging-interceptor:$okhttp_version" 添加到您的 gradle 文件中,并将您的改造设置更改为以下内容:

    val httpClient = OkHttpClient.Builder().apply {
        if (BuildConfig.DEBUG) {
            httpClient.addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })
        }
    }
    
    var retrofit = Retrofit.Builder()
            .client(httpClient.build())
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    

    这是我可以在不查看错误日志的情况下给出的最佳建议。

    【讨论】:

      【解决方案2】:

      我在 POST 请求中的网址末尾缺少“/”。 从

      更改网址

      www.example.com/list_products

      到 www.example.com/list_products/ 为我工作。

      【讨论】:

        【解决方案3】:

        对我来说,主要原因是我调用 http 而不是 https 例如: 我使用这个 url 作为一个基本 url,这给了我这个错误

        http://www.facebook.com/
        

        当我把它改成这个时,它就像一个魅力

        https://www.facebook.com/
        

        【讨论】:

        • 非常感谢这是我的问题
        【解决方案4】:

        当我们没有发送我们的请求方法或发送错误的请求方法时,会显示状态码为 405 的方法不允许错误。

        在我的场景中,Api 是 POST 类型,我正在检查 GET 请求类型,

        所以尝试更改您的正确请求方法(GET/POST/PUT/DELETE)。

        【讨论】:

          【解决方案5】:

          我找到了答案,问题是url,我的baseUrl是http//www.example.com/url1/url2/...../service@POST("/")

          当我看到 POST 端点时,它是 http//www.example.com/,我不知道为什么

          我刚刚更改了这个网址 baseUrl = http//www.example.com/@POST("url1/url2/...../service")

          【讨论】:

          • 对我来说是类似的。这是由于我的 Web 服务器配置错误导致 http 重定向到 https,但该方法未传输,我不小心调用了 http url 进行测试。以防万一将来有人偶然发现同样的问题。
          猜你喜欢
          • 2021-12-20
          • 2012-09-30
          • 2018-09-08
          • 2020-06-17
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          • 2019-05-18
          相关资源
          最近更新 更多