【发布时间】:2017-08-23 19:00:30
【问题描述】:
我正在尝试使用 GET 请求向 bing 的拼写检查 api 发送一个 http 请求。我在https://www.hurl.it/ 上检查了我的参数和标题,它正确地返回了带有拼写错误的正确 json,但是当我从我的 java 应用程序发送请求时,它返回这个 json,没有检测到拼写错误(因此,文本参数必须为空不知何故)。我肯定在标头中传递了正确的密钥,因为该部分没有发送错误并且代码是 200(成功)。
我的字符串:“my funger is harting me”
我的代码返回:
{"_type":"SpellCheck","flaggedTokens":[]}
Hurl.it 返回:
{
"_type":"SpellCheck",
"flaggedTokens":[
{
"offset":3,
"token":"funger",
"type":"UnknownToken",
"suggestions":[
{
"suggestion":"finger",
"score":0.903614003311793
}
]
},
{
"offset":13,
"token":"harting",
"type":"UnknownToken",
"suggestions":[
{
"suggestion":"hurting",
"score":0.903614003311793
}
]
}
]
}
这是我使用 Apache 的 HTTPClient 库的 java 代码: (注意:“command.getAfter()”是我上面提到的传递字符串。我调试了它,甚至硬编码了一个字符串来测试它。显然相同的输出。)
HttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/");
builder.setParameter("text", command.getAfter());
URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.setHeader("Ocp-Apim-Subscription-Key", "XXXXXXXX");
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
编辑:原来在请求对象中返回的 URI 是这样的: https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?text=my+funger+is+harting+me
所以参数不为空?但是当 hurl.it 中没有输入文本参数时,api 会返回没有参数的错误。当文本参数是空格“”时,它返回一个相同的结果给我。不确定这意味着什么,因为 URI 似乎有效且不为空,而且我的订阅密钥正在工作,因为如果不是,我会收到错误...
编辑:我开始怀疑 Apache 库忽略了我在 HttpGet(uri) 中传递的参数。我不确定,但我将尝试不同的解决方案来发送带有标头的请求,看看会发生什么。
编辑:我尝试了以下代码:
String url = "https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?text=" + command.getAfter().replace(" ", "+");
try {
URL request_url = new URL(url);
//URIBuilder uri = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/");
//uri.setParameter("text", command.getAfter());
HttpURLConnection con = (HttpURLConnection) request_url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Ocp-Apim-Subscription-Key", Keys.BING_SPELL_CHECK_API);
con.setConnectTimeout(100000);
con.setReadTimeout(100000);
con.setInstanceFollowRedirects(true);
String theString = IOUtils.toString(con.getInputStream(), "UTF-8");
System.out.println(theString);
} catch (IOException e) {
e.printStackTrace();
}
它返回与 Apache 相同的结果...:/ 我还应该尝试什么?
编辑: 这也是请求的输出:
https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?text=my+funger+is+hartingme - [Ocp-Apim-Subscription-Key: <XXXXXXXXXXXX>]
HTTP/1.1 200 OK - en_US
{"_type": "SpellCheck", "flaggedTokens": []}
我不明白.... 为什么当 hurl.it 返回相同请求的正确 json 时 json 输出为空?这是java问题还是什么?
编辑: 我刚刚尝试了 UniRest 的 api。完全相同的结果......我在这里做错了什么?! 我迷路了……
单独的问题: 我确实想注意以下几点:当我将 bing api 的版本设置为 7.0 时,我收到以下错误:
Received http status code 401 with message Access Denied and body {"message":"Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription.","statusCode":401}
v5.0 不是这种情况。我正在从我的 Azure 门户获取正确的密钥。 (名为 Keys 的页面列出了您可以使用和重新生成的 2 个密钥)
【问题讨论】:
-
request.getURI()的输出是什么? -
@JeremiahMegel 我刚刚检查过。 api.cognitive.microsoft.com/bing/v5.0/spellcheck/…我是不是传参数不正确?
-
您的代码对我有用。我正在使用 JDK 1.8 和
httpclient-4.5.3.jar和httpcore-4.4.7.jar和commons-logging-1.2.jar
标签: java apache http bing microsoft-cognitive