wit.ai - 如何用Java发送请求? [英] wit.ai - How to send request in Java?
本文介绍了wit.ai - 如何用Java发送请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Java中的wit.ai开发一个虚拟助手,但我仍然无法发出HTTP请求。我不是Java中的HTTP请求专家,我一直收到400错误。
I'm working on a virtual assistent using wit.ai in Java, but I'm stuck making the HTTP request. I'm no pro at HTTP requests in Java and I get a error 400 the whole time.
这是我的代码:
public class CommandHandler {
public static String getCommand(String command) throws Exception {
String url = "https://api.wit.ai/message";
String key = "TOKEN HERE";
String param1 = "20141022";
String param2 = command;
String charset = "UTF-8";
String query = String.format("v=%s&q=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty ("Authorization Bearer", key);
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
return response.toString();
}
}
这是 wit.ai 给出的示例:
$ curl \
-H 'Authorization: Bearer $TOKEN' \
'https://api.wit.ai/message?v=20141022&q=hello'
我希望有人可以帮助我
推荐答案
这对我很有帮助!
connection.setRequestProperty ("Authorization": "Bearer "+key);
这篇关于wit.ai - 如何用Java发送请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文