public static string Translate(string sInput) { if (string.IsNullOrEmpty(sInput)) return ""; try { HttpClient hClient = new HttpClient(); sInput = System.Web.HttpUtility.UrlEncode(sInput); string sReq = "http://translate.google.ru/translate_a/t?client=t&text=" + sInput + "&hl=ja&sl=ja&tl=en&multires=1&otf=2&trs=1&ssel=0&tsel=0&sc=1"; string sResp = hClient.DownloadString(sReq); sResp = sResp.Substring(4); sResp = sResp.Substring(0, sResp.IndexOf("\",\"")); sResp = sResp.Replace("\\\"", "\""); return sResp; } catch (System.Exception e) { Console.WriteLine(e.Message); } return ""; }
public static string Recognize(Bitmap inputBitmap) { MemoryStream ms = new MemoryStream(); inputBitmap.Save(ms, ImageFormat.Bmp); byte[] bmp = ms.ToArray(); HttpClient hClient = new HttpClient(); //Make POST-request string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("d").Substring(0, 13); string NameAffix = "--" + boundary + "\r\nContent-Disposition: form-data; name=\""; System.IO.MemoryStream postdata = new System.IO.MemoryStream(); //Header of file string formdata = ""; formdata += NameAffix + "userfile\"; filename=\"image.bmp\"\r\n"; formdata += "Content-Type: image/bmp\r\n\r\n"; //Write postdata.Write(Encoding.ASCII.GetBytes(formdata), 0, formdata.Length); postdata.Write(bmp, 0, bmp.Length); //Prepare ending formdata = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"outputencoding\"\r\n\r\nutf-8\r\n"; formdata += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"outputformat\"\r\n\r\ntxt\r\n"; formdata += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"eclass\"\r\n\r\nauto\r\n"; formdata += "--" + boundary + "--"; //Write postdata.Write(Encoding.ASCII.GetBytes(formdata), 0, formdata.Length); byte[] buffer = new byte[postdata.Length]; postdata.Seek(0, System.IO.SeekOrigin.Begin); postdata.Read(buffer, 0, buffer.Length); System.IO.File.WriteAllBytes("log.txt", buffer); hClient.Timeout = hClient.Timeout * 10; hClient.Referer = "http://appsv.ocrgrid.org/nhocr/"; return hClient.UploadMultipartData( "http://appsv.ocrgrid.org/cgi-bin/weocr/nhocr.cgi", buffer, boundary); }
private string Translate(string str) { HttpClient hClient = new HttpClient(); str = System.Web.HttpUtility.UrlEncode(str); string sReq = "http://translate.google.ru/translate_a/t?client=t&text=" + str + "&hl=ja&sl=ja&tl=en&multires=1&otf=2&trs=1&ssel=0&tsel=0&sc=1"; string sResp = hClient.DownloadString(sReq); sResp = sResp.Substring(4); sResp = sResp.Substring(0, sResp.IndexOf("\",\"")); sResp = sResp.Replace("\\\"","\""); return sResp; }