public string Translate(string from, string to, string text) { String myMemoryBaseUrl = "http://api.mymemory.translated.net/get?"; String getParams = String.Format("q={0}&langpair={1}|{2}", HttpUtility.UrlEncode(text), HttpUtility.UrlEncode(from), HttpUtility.UrlEncode(to)); String optionalEmailParam = ""; if ((settings.email != null) && (settings.email.Length > 0)) { optionalEmailParam = "&de=" + HttpUtility.UrlEncode(settings.email); } String finalUrl = myMemoryBaseUrl + getParams + optionalEmailParam; MyMemoryResponseModel response = makeGETRequest(finalUrl); if (response.responseStatus.Equals("200")) { return(response.responseData.translatedText); } else { throw new Exception(response.responseData.translatedText); } }
private MyMemoryResponseModel makeGETRequest(String url) { Util.writeInfoLog(url); //Prepare OAuth request WebRequest webRequest = WebRequest.Create(url); webRequest.Method = "GET"; using (WebResponse webResponse = webRequest.GetResponse()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyMemoryResponseModel)); //Get deserialized object from JSON stream MyMemoryResponseModel mymemoryResp = (MyMemoryResponseModel)serializer.ReadObject(webResponse.GetResponseStream()); return(mymemoryResp); } }