call translator timeout sometimes

cloudact 21 Reputation points
2020-12-21T01:32:57.85+00:00

Exception:
java.net.SocketTimeoutException: timeout

call example:
public class Translate {
private static String subscriptionKey = "YOUR_SUBSCRIPTION_KEY";

// Add your location, also known as region. The default is global.
// This is required if using a Cognitive Services resource.
private static String location = "YOUR_RESOURCE_LOCATION";

HttpUrl url = new HttpUrl.Builder()
    .scheme("https")
    .host("api.cognitive.microsofttranslator.com")
    .addPathSegment("/translate")
    .addQueryParameter("api-version", "3.0")
    .addQueryParameter("from", "en")
    .addQueryParameter("to", "de")
    .addQueryParameter("to", "it")
    .build();

// Instantiates the OkHttpClient.
OkHttpClient client = new OkHttpClient();

// This function performs a POST request.
public String Post() throws IOException {
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType,
            "[{\"Text\": \"Hello World!\"}]");
    Request request = new Request.Builder().url(url).post(body)
            .addHeader("Ocp-Apim-Subscription-Key", subscriptionKey)
            .addHeader("Ocp-Apim-Subscription-Key", location)
            .addHeader("Content-type", "application/json")
            .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}

// This function prettifies the json response.
public static String prettify(String json_text) {
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(json_text);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    return gson.toJson(json);
}

public static void main(String[] args) {
    try {
        Translate translateRequest = new Translate();
        String response = translateRequest.Post();
        System.out.println(prettify(response));
    } catch (Exception e) {
        System.out.println(e);
    }
}

}

Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
393 questions
{count} votes

Accepted answer
  1. romungi-MSFT 45,961 Reputation points Microsoft Employee
    2020-12-21T10:53:12.593+00:00

    @cloudact It looks like you are using the header Ocp-Apim-Subscription-Key header twice with a location and subscription key values. If you are using a regional translator resource please change one header key to Ocp-Apim-Subscription-Region.

    The most common cause for a timeout could be the length of the body or characters in the request which in turn are limited by the tier of your translator resource. If you are using a lower tier of the resource like F0(Free) you can upgrade to a higher tier S1 and above and check if the same requests are timing out. Also, you can breakdown your requests to smaller requests to ensure there is no latency or timeout. A sample from the V3 API for Java is also available here for reference.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.