請(qǐng)求中編碼問(wèn)題
API請(qǐng)求編碼問(wèn)題
客戶端請(qǐng)求API網(wǎng)關(guān)時(shí),需要對(duì)參數(shù)值進(jìn)行utf-8的urlEncode,這樣能避免特殊參數(shù)或者中文出現(xiàn)亂碼。 注意:query、header、body位置編碼需要在簽名計(jì)算后,簽名時(shí)用原始值。
以下代碼可參考:https://github.com/aliyun/api-gateway-demo-sign-java。
1、header值進(jìn)行編碼
header的編碼和value編碼不太一樣,header的值需要用ISO-8859-1編碼。
HttpPost post = new HttpPost(initUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
//header的傳值,value要編碼后傳,具體實(shí)現(xiàn)請(qǐng)看下面
post.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
}
/**
* UTF-8編碼轉(zhuǎn)換為ISO-8859-1 *
* @param str
* @return
*/
public static String utf8ToIso88591(String str) {
if (str == null) {
return str;
}
try {
return new String(str.getBytes("UTF-8"), "ISO-8859-1"); }
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
2、query值進(jìn)行編碼
如果query參數(shù)值含中文,特殊符號(hào),如“+”號(hào)等,都需要對(duì)query的值進(jìn)行utf-8編碼。可參考下面URL的構(gòu)建方式。
private static String initUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (!StringUtils.isBlank(path)) {
sbUrl.append(path);
}
if (null != querys) {
StringBuilder sbQuery = new StringBuilder();
for (Map.Entry<String, String> query : querys.entrySet()) {
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
sbQuery.append(query.getValue());
}
if (!StringUtils.isBlank(query.getKey())) {
sbQuery.append(query.getKey());
if (!StringUtils.isBlank(query.getValue())) {
sbQuery.append("=");
sbQuery.append(URLEncoder.encode(query.getValue(), "UTF-8"));
}
}
}
if (0 < sbQuery.length()) {
sbUrl.append("?").append(sbQuery);
}
}
return sbUrl.toString();
}
3、body參數(shù)進(jìn)行編碼
1)form形式body的編碼:
UrlEncodedFormEntity formEntity = buildFormEntity(bodys);
if (formEntity != null) {
post.setEntity(formEntity);
}
/**
* 構(gòu)建FormEntity
*
* @param formParam
* @return
* @throws UnsupportedEncodingException
*/
private static UrlEncodedFormEntity buildFormEntity(Map<String, String> formParam)
throws UnsupportedEncodingException {
if (formParam != null) {
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
for (String key : formParam.keySet()) {
nameValuePairList.add(new BasicNameValuePair(key, formParam.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8");
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
return formEntity;
}
return null;
}
2)非form形式的body
String 形式的body:
if (StringUtils.isNotBlank(body)) {
post.setEntity(new StringEntity(body, "UTF-8"));
}
byte[]形式的body:
if (bodys != null) {
post.setEntity(new ByteArrayEntity(bodys));
}
4、path參數(shù)編碼
當(dāng)API的path中有參數(shù),而且參數(shù)含特殊字符。需要先將參數(shù)urlencode后加入path中。不然簽名會(huì)有問(wèn)題。此時(shí)后端收到的也是urlEncode后的值,如果要獲取原值,需要自己在后端進(jìn)行urlDecode。
@Test
public void testPath() throws Exception{
//請(qǐng)求path
String host=“你的域名”;
//請(qǐng)求path,先將參數(shù)處理后再放入path中。
String type=“中文 123”;
String pathParam= URLEncoder.encode(type,"UTF-8");
String path = "/"+pathParam;
Map<String, String> headers = new HashMap<String, String>();
//(必填)根據(jù)期望的Response內(nèi)容類型設(shè)置
headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
Request request = new Request(Method.GET, host, path, AppKey, AppSecret, Constants.DEFAULT_TIMEOUT);
request.setHeaders(headers);
request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
//調(diào)用服務(wù)端
Response response = Client.execute(request);
System.out.println(JSON.toJSONString(response));
}
5、參數(shù)中包含emoji表情
當(dāng)參數(shù)中包含emoji表情,需要先對(duì)參數(shù)值進(jìn)行urlencode后再簽名,不然簽名不過(guò),因?yàn)锳PI網(wǎng)關(guān)的系統(tǒng)可能會(huì)識(shí)別不了該符號(hào),會(huì)導(dǎo)致簽名失敗,所以需要先進(jìn)行urlencode處理后再簽名。
此時(shí)后端收到的為urlencode后的字符串,如果要獲取原文,后端需要自己做urldecode。