String jsonOutput = null;
String urlStr = null;
try {
String encodedInputText = "query={\"query\":\"" +URLEncoder.encode(question, "UTF-8") + "\"";
if(sku !=null){
encodedInputText += ",\"sku\":\"" + sku + "\"}";
}
urlStr = Parameters.getStsWebserviceURL();
URL url = new URL(urlStr + encodedInputText);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF8"));
jsonOutput = in.readLine();
in.close();
LOGGER.info("{} {} is computed by sts {}", sku, question, urlStr);
}catch(IOException e){
...
}
encodedText是:
{"query":"这款是大一匹的呀","sku":"4265252"}
编码后的“ url”字符串值为:
http://xx.xx.92.203/gt/shortTextSearch?query={"query":"%E8%BF%99%E6%AC%BE%E6%98%AF%E5%A4%A7%E4%B8%80%E5%8C%B9%E7%9A%84%E5%91%80","sku":"4265252"}
如果我将此网址粘贴到chrome浏览器中,它将返回预期的结果。但是在上面的代码中,它会产生IOException并显示以下消息:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://xx.xx.203/gt/shortTextSearch?query={"query":"%E8%BF%99%E6%AC%BE%E6%98%AF%E5%A4%A7%E4%B8%80%E5%8C%B9%E7%9A%84%E5%91%80","sku":"4265252"}