为什么Retrofit会忽略我的URL请求中的某些字符? [英] Why is Retrofit omitting certain characters from my URL request?

查看:159
本文介绍了为什么Retrofit会忽略我的URL请求中的某些字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 NOT 问题与需要对网址进行编码有关。这是由Retrofit完成的。如果我对URL进行编码,然后将其传递给Retrofit,则URL会被编码两次,最终看起来像这样:

Update: The issue is NOT to do with needing to encode the URL. This is done by Retrofit. If I encode the URL, and then pass it to Retrofit, the URL gets encoded twice and ends up looking like this:

https://example.com/api/get_user_info/?id=12345&cookie=user@email.com%257C1492357107%257CfyVzRUYC9h%257C4f889e1976c2cd87aac

请注意%257C 。第一轮是 | - > %7C 。第二轮是%7C - > %257C

Note the %257C. First round is | -> %7C. Second round is %7C -> %257C

我正在尝试使用Retrofit执行HTTP GET请求。请求如下所示:
https://example.com/api/get_user_info/?id=12345&cookie=user@email .com | 1492357107 | fyVzRUYC9h | 4f889e1976c2cd87aac

I am trying to execute an HTTP GET request using Retrofit. The request looks like this: https://example.com/api/get_user_info/?id=12345&cookie=user@email.com|1492357107|fyVzRUYC9h|4f889e1976c2cd87aac

在代码中,通话看起来像这样

In code, the call looks like this

@GET("get_user_info")
Call<ResponseBody> getUserMeta(
            @Query("id") int userId,
            @Query("cookie") String cookie
);

当我执行此查询时,请求看起来像这样:

When I execute this query, the Request looks like this:

Request{method=GET, url=https://example.com/api/get_user_info/?id=12345&cookie=user@email.com|1492357107|fyVzRUYC9h|4f889e1976c2cd87aac, tag=null}

(在记录 call.request()。toString()后从控制台获取)

(Taken from the console after logging call.request().toString())

执行调用后,我收到API的错误,说明cookie无效。响应如下所示:

After the call is executed, I receive an error from my API saying that the cookie is invalid. The Response looks like this:

Response{protocol=h2, code=404, message=, url=https://example.com/api/get_user_info/?id=12345&cookie=user@email.com1492357107fyVzRUYC9h4f889e1976c2cd87aac}

(在loggin之后从控制台获取响应< ResponseBody> response.toString()

(Taken from the console after loggin Response<ResponseBody> response.toString())

如您所见,两个网址不一样。不知何故, | 字符已从响应网址中的cookie参数中删除。

As you can see, the two URLs are not the same. The | character has somehow been removed from the cookie parameter in the response's URL.

您可能认为这有与我的API有关,并且cookie实际上是不正确的,但是如果我将 Request URL直接复制并粘贴到我的浏览器中,它就会执行而没有错误(状态200)。如果Retrofit执行调用,我会得到404。

You might think that this has something to do with my API, and that the cookie is in fact incorrect, but if I copy and paste the Request URL directly into my browser, it executes with no error (status 200). If Retrofit executes the call, I get 404.

显然,Retrofit正在执行第二个不正确的URL。这显然是这种情况,因为第一个URL在浏览器中执行时不会返回404,但第二个URL会返回404.

Evidently, Retrofit is executing the second, incorrect URL. This is clearly the case because the first URL does not return a 404 when executed in the browser, but the second one does.

发生了什么?任何帮助将不胜感激。

What is going on? Any help would be much appreciated.

其他信息:

我正在使用编译'com.squareup.retrofit2:retrofit:2.2.0'在我的app模块的build.gradle中。

I am using compile 'com.squareup.retrofit2:retrofit:2.2.0' in my app module's build.gradle.

我正在实例化我的Retrofit客户端所以:

I am instantiating my Retrofit client like so:

Retrofit retroFit = new Retrofit.Builder()
                    .baseUrl("https://example.com/api/")
                    .build();


推荐答案

你应该实现 encoding 你的cookies:

You should implement encoding on your cookies :

@FormUrlEncoded
@GET("get_user_info")
Call<ResponseBody> getUserMeta(
    @Query("id") int userId,
    @Query(value ="cookie", encode = true) String cookie

    );

这篇关于为什么Retrofit会忽略我的URL请求中的某些字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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