aliyun / csb-sdk

The CSB-SDK is a client-side invocation SDK for HTTP or Web Service API opened by the CSB (Cloud Service Bus) product. It is responsible for invoking the open API and signing the request information.
Other
63 stars 48 forks source link

如果要设置HTTP HEADER里的值是中文怎么办? #4

Closed walking98 closed 6 years ago

walking98 commented 6 years ago

我的客户端代码里设置http header的值为中文的时候,如取值为: "姓名" 会报如下的错误:

HttpResponseProxy{HTTP/1.1 400 Bad Request [Connection: close, Server: Jetty(8.1.17.v20150415)] ResponseEntityProxy{[Chunked: false]}}

解释: 发现header中只能传输英文,如果需要传输中文,发放方需要使用URLEncoder.encode(“我是汉字”,"UTF-8") 进行编码,接收方需要使用URLDecoder.decode("待解析字符串", "UTF-8")进行解码!

客户端代码实例:

    @Test
    public void testHeader() {
        try {
            HttpParameters.Builder hp = HttpParameters.newBuilder();
            hp.api("lt-http2http2").requestURL("http://localhost:8086/CSB");
            hp.version("1.0.0");
            //hp.method("post");
            hp.putParamsMap("arg0", "abc");
            hp.putParamsMap("pv", "10");

            //将Header里的值进行中文urlEncode
            hp.putHeaderParamsMap("test", URLEncoder.encode("姓名","UTF-8"));
            String ret = HttpCaller.invoke(hp.build());
            System.out.println("res = "+ ret);

        }catch(Exception e) {
            e.printStackTrace(System.out);
        }
    }
walking98 commented 6 years ago

do just like this.