简体   繁体   English

OkHttp为什么不对百分号进行编码?

[英]Why doesn't OkHttp encode the percent sign?

From HttpUrl.java : 来自HttpUrl.java

static final String FORM_ENCODE_SET = " \"':;<=>@[]^`{}|/\\?#&!$(),~";

From FormBody.java FormBody.java

public Builder addEncoded(String name, String value) {
      names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true, charset));
      values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, false, true, true, charset));
      return this;
    }

A side effect here is that when we post a login form (username and password), if the password contains a '%' (percent sign), then it is not encoded and the user can not log in. Why is this character not encoded by OkHttp? 这样做的副作用是,当我们发布登录表单(用户名和密码)时,如果密码包含“%”(百分号),则表示该密码未编码且用户无法登录。为什么不对该字符进行编码由OkHttp吗?

For reference, here is how we are creating the form (the map contains two strings, username and password): 供参考,这是我们如何创建表单(地图包含两个字符串,用户名和密码):

public RequestBody createEncodedFormBody(final Map<String, String> content) {
        final FormBody.Builder builder = new FormBody.Builder();
        for (final Entry<String, String> contentEntry : content.entrySet()) {
            builder.addEncoded(contentEntry.getKey(), contentEntry.getValue());
        }
        return builder.build();
    }

Use FormBody.Builder.add() instead of addEncoded() . 使用FormBody.Builder.add()代替addEncoded() The encoded method assumes you've already encoded the input. 编码方法假定您已经编码了输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM