我收到400条响应代码,代码如下:
String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection();
authConn.setRequestMethod("POST");
authConn.setDoOutput(true);
authConn.setRequestProperty("Ocp-Apim-Subscription-Key", key);
IOUtils.write("", authConn.getOutputStream(), "UTF-8");
String token = IOUtils.toString(authConn.getInputStream(), "UTF-8");
// Using the access token to build the appid for the request url
String appId = URLEncoder.encode("Bearer "+token, "UTF-8");
String[] languages = {"ar","bg","ca","zh","cs", "da","nl", "fi","fr","de","el","hu", "id", "it", "ja",
"ko","ms","no", "pl","pt","ro","ru", "es","sw","th","tr","uk","vi"};
US_LISTING_FULL_DESCRIPTION = URLEncoder.encode(readFile("d:\\desc1.txt", Charset.defaultCharset()), "UTF-8");
System.out.println("languages.length " + languages.length);
WrapperBean[] wrapperBean = new WrapperBean[languages.length];
try{
for (int i = 0; i < languages.length; i++) {
wrapperBean[i] = new WrapperBean();
wrapperBean[i].setLocale(languages[i]);
System.out.println("languages[i] " + languages[i]);
String url1 = String.format("https://api.microsofttranslator.com/v2/http.svc/Translate?appid=%s&text=%s&from=%s&to=%s", appId, US_LISTING_TITLE, "en", languages[i]);
HttpsURLConnection translateConn = (HttpsURLConnection) new URL(url1).openConnection();
translateConn.setRequestMethod("GET");
translateConn.setRequestProperty("Accept", "application/xml");
String translatetitle = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
上面代码的最后一行是我收到错误的地方。
Eclipse控制台中的确切错误是:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.microsofttranslator.com/v2/http.svc/Translate?appid=Bearer+eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6Imh0dHBzOi8vYXBpLm1pY3Jvc29mdHRyYW5zbGF0b3IuY29tLyIsInN1YnNjcmlwdGlvbi1pZCI6ImI0MDk0NjcwMDJkMzQ1OTVhZDljNWI3ZDA1NTcxMGNjIiwicHJvZHVjdC1pZCI6IlRleHRUcmFuc2xhdG9yLkYwIiwiY29nbml0aXZlLXNlcnZpY2VzLWVuZHBvaW50IjoiaHR0cHM6Ly9hcGkuY29nbml0aXZlLm1pY3Jvc29mdC5jb20vaW50ZXJuYWwvdjEuMC8iLCJhenVyZS1yZXNvdXJjZS1pZCI6Ii9zdWJzY3JpcHRpb25zLzExMTk0NjkyLTk1ZGEtNGU5Yi1hMDVhLTBiZjg0YTcxYzM3OC9yZXNvdXJjZUdyb3Vwcy9jc3MvcHJvdmlkZXJzL01pY3Jvc29mdC5Db2duaXRpdmVTZXJ2aWNlcy9hY2NvdW50cy9jc3MiLCJpc3MiOiJ1cm46bXMuY29nbml0aXZlc2VydmljZXMiLCJhdWQiOiJ1cm46bXMubWljcm9zb2Z0dHJhbnNsYXRvciIsImV4cCI6MTUwMTEzMDk2OX0.JPgvzprydjz5_cVi3pk23X1VxgmlqbcoL4bPADkxqYA&text=Acoustic Guitar Tuners&from=en&to=ar
请帮助。
答案 0 :(得分:3)
HTTP状态代码400是&#34;错误请求&#34;。您的查询有问题。 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors
问题是AFAIK,您的请求包含无效的空格。
&amp; text =原声吉他调谐器&amp; from = en&amp; to = ar
您必须将空格替换为例如&#34; +&#34;,像这样:
&安培;文本=声学吉他+ +调谐器和放大器;从= EN&安培;要= AR
我希望这能解决你的问题。祝你好运!