【问题标题】:400 response code with Microsoft Translation in Java带有 Microsoft 翻译的 Java 400 响应代码
【发布时间】:2018-01-02 15:24:20
【问题描述】:

我收到 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

这样的 URL 在浏览器中有效:

请帮忙。

【问题讨论】:

    标签: java web-services rest azure


    【解决方案1】:

    HTTP 状态代码 400 是“错误请求”。你的查询有问题。 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors

    问题是 AFAIK,您的请求包含无效的空格。

    &text=原声吉他调音器&from=en&to=ar

    您必须用 for 替换空格,例如“+”,像这样:

    &text=原声+吉他+调音器&from=en&to=ar

    Spaces in URLs?

    我希望这能解决您的问题。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      相关资源
      最近更新 更多