Java URL - Google Translate request returns 403 error? [duplicate]

馋奶兔 提交于 2019-12-06 11:33:45

They are looking at the user agent string, and presumably they don't want people doing this programatically.

I did get your code working working, but since Google charges for the API access and they are actively blocking things that are not browsers (based on the user agent string) I won't tell you how I did it.

A google search for setting the user agent string in Java will get you what you want (as a matter of fact I found the answer here on Stackoverflow).

deceiving "translate.google" by telling that I'm a browser not a running code.

URLConnection conn = url.openConnection();
// fake request coming from browser
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");

Add referer to request like

    URL translateURL = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) translateURL
            .openConnection();

    connection.setDoOutput(true);
    connection.setRequestProperty("X-HTTP-Method-Override", "GET");
    connection.setRequestProperty("referer", "accounterlive.com");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!