400 response code with Microsoft Translation in Java

I am getting 400 response code with following code:

    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");

The last line in above code is where I get the error.

The exact error in Eclipse console is:

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

The URL as such works in the browser: enter image description here

Kindly help.

Upvotes: 0

Views: 189

Answers (1)

Yannick
Yannick

Reputation: 843

HTTP status code 400 is "Bad Request". Your query is faulty. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors

The problem is AFAIK that your request contains spaces which are invalid.

&text=Acoustic Guitar Tuners&from=en&to=ar

You have to replace the spaces with for e.g. "+", like this:

&text=Acoustic+Guitar+Tuners&from=en&to=ar

See Spaces in URLs?

I hope this solves your problem. Good luck!

Upvotes: 3

Related Questions