Hello all,
I registered the account of the Google translation, and the translation of the API key, but in the translation, but reported URL 403 error, why so, how to solve?
My Code is:
private string BuildPostData(params string[] source)
{
const string QUERY_PARAM = "q";
StringBuilder data = new StringBuilder();
foreach (var item in source)
{
data.AppendFormat("{0}={1}&", QUERY_PARAM, HttpUtility.UrlEncode(item));
}
if (data.Length > 0)
{
data.Remove(data.Length - 1, 1);
}
return data.ToString();
}
private GoogleTranslateResult PostDataToTranslate(params string[] source)
{
string data = BuildPostData(source);
Encoding encoding = Encoding.UTF8;
byte[] bytesToPost = encoding.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TranslateUrl);
request.Headers.Add("X-HTTP-Method-Override", "GET");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytesToPost.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
}
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream, encoding))
{
string jsonData = sr.ReadToEnd();
GoogleTranslateResult result = JsonConvert.DeserializeObject<GoogleTranslateResult>(jsonData);
result.SourceData = source;
return result;
}
}
}
}
Thank you!