本文介绍了如果 url 字符串包含“~",则改造调用错误的 url;符号导致 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了改造客户端和 api 接口,但改造跳过了 url 中的某些部分并调用了错误的 url

I have created retrofit client and api interface but retrofit skipping some part in url and calling wrong url

这里是完整的网址http://192.168.0.201/~amol/eflbudget/budgetmanagements/test

改造调用的网址http://192.168.0.201/budgetmanagements/test/

这会导致 404 错误

this causing 404 error

//here is my retrofit client code

public static Retrofit getClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .build();

    if (retrofit != null) {
        return retrofit;
    }

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.0.201/")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(client)
            .build();

    return retrofit;
}

// below is api call
@Multipart
@POST("/~amol/eflbudget/budgetmanagements/test/")
Call<ResponseBody> postMail(
        @Part("user_id") RequestBody userId,
        @Part("lead_id") RequestBody leadId,
        @Part("to") RequestBody to,
        @Part("cc_to") RequestBody ccId,
        @Part("template_id") RequestBody tempId,
        @Part("sender") RequestBody senderId,
        @Part("subject") RequestBody subject,
        @Part("message") RequestBody message,
        @Part MultipartBody.Part file
);

预期的 api 网址:http://192.168.0.201/~amol/eflbudget/budgetmanagements/测试

expected api url : http://192.168.0.201/~amol/eflbudget/budgetmanagements/test

实际网址:http://192.168.0.201/budgetmanagements/test/

这里/~amol/eflbudget url 中的这部分被忽略
有人可以在这里提出建议吗?

here /~amol/eflbudget this part in url getting ignored
can anyone suggest somethings here?

推荐答案

RFC以下字符:

非保留字符

URI 中允许但没有保留的字符目的被称为无保留.这些包括大写和小写字母、十进制数字、连字符、句点、下划线和波浪号.

Characters that are allowed in a URI but do not have a reserved purpose are called unreserved. These include uppercase and lowercase letters, decimal digits, hyphen, period, underscore, and tilde.

  unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"

伯纳斯-李等人.标准跟踪 [第 13 页]

Berners-Lee, et al. Standards Track [Page 13]

RFC 3986 URI 通用语法 2005 年 1 月

RFC 3986 URI Generic Syntax January 2005

不同的 URI 将非保留字符替换为其对应的百分比编码的 US-ASCII 八位字节是等效的:它们识别相同的资源.但是,URI 比较实现不要总是在比较之前执行归一化(参见部分6).为了保持一致性,百分比编码的八位字节在 ALPHA 范围内(%41-%5A 和 %61-%7A)、数字 (%30-%39)、连字符 (%2D)、句点 (%2E)、下划线 (%5F) 或波浪号 (%7E) 不应由 URI 创建生产者,当在 URI 中找到时,应将其解码为它们的URI 规范器对应的未保留字符.

URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent: they identify the same resource. However, URI comparison implementations do not always perform normalization prior to comparison (see Section 6). For consistency, percent-encoded octets in the ranges of ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers.

这篇关于如果 url 字符串包含“~",则改造调用错误的 url;符号导致 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 16:09