wit.ai – 如何用Java发送请求?
发布时间:2020-12-15 02:13:04 所属栏目:Java 来源:网络整理
导读:我正在使用 Java中的wit.ai开发一个虚拟助手,但是我无法发出HTTP请求.我不是Java中的HTTP请求的专家,我一直得到400错误. 这是我的代码: public class CommandHandler {public static String getCommand(String command) throws Exception { String url = "h
|
我正在使用
Java中的wit.ai开发一个虚拟助手,但是我无法发出HTTP请求.我不是Java中的HTTP请求的专家,我一直得到400错误.
这是我的代码: 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' 我希望有人可以帮助我 解决方法
下面是快速检查您的休息api的简单代码
try {
URL url = new URL("https://api.wit.ai/message?v=20170218&q=Hello");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept","application/json");
conn.setRequestProperty("Authorization","Bearer addkeyhere");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – 处理IllegalThreadStateException
- Eclipse Key Shortcuts for Greater Developers Productivi
- java – XML vs JSON.哪一个更适合存储小块数据?
- java – 尝试使用Smack结果登录到XMPP服务器在SASL“未授权
- java – Android数据绑定测试在模块NoClassDefFoundError中
- 一个很好的java(tomcat)SOAP库
- java – Spring Boot安全性在登录失败后显示Http-Basic-Aut
- java – 从12升级到Intellij 13.1后,Maven项目将无法编译
- 深入浅析 Spring Boot Starter
- 【面向对象设计的3个基本特征】
