The HTTP header line [Content-Type: application/json: charset=utf-8] does not conform to RFC 7230 an

最近项目中遇到一件怪事,在POST请求中发现没有数据,在debug时候发现居然爆了一个retrofit2.adapter.rxjava.HttpException: HTTP 400错误。经过查阅资料发现400 请求出错
  由于语法格式有误,服务器无法理解此请求。这个错误很奇葩。把请求的完整参数发到postman中请求发现没有问题。包括使用原生的Http 写都没有问题,正在觉得怪异的时候,请求参数中|这个特殊字符串引起了我的注意。
通过在OKhttp 拦截器中打印出本次请求路径发现,所有的参数都是被编码了,但是|这个特殊字符却没有编码。

HTTP Status 400 – Bad Request

HTTP Status 400 – Bad Request


Type Exception Report

Message The HTTP header line [application/json: charset=utf-8] does not conform to RFC 7230 and has been ignored.

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Exception

java.lang.IllegalArgumentException: The HTTP header line [Content-Type: application&json: charset=utf-8] does not conform to RFC 7230 and has been ignored.

 

以下是示例修改代码
/**
 * Created by J.query on 2017/6/27.
 * email [email protected]
 */

public class HeaderInterceptor implements Interceptor {

    private Context context;

    public HeaderInterceptor(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        String name = "application/json";
        try {
            name = URLEncoder.encode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Request request = original.newBuilder()
                .header(name, "charset=utf-8")
                .method(original.method(), original.body())
                .build();
        return chain.proceed(request);
    }
}

这里说一下由于Toamcat 9.0对字符做了校验,导致 "application/json" 中的“/”校验不过,所以呢

给字符处理一下就OK了。

 

你可能感兴趣的:(BUG处理,RFC,7230,HTTP,header,line,URLEncoder,Android接口调试)