本帖最后由 angelkyo 于 2018-3-30 14:53 编辑
成品:
BaiduTranslate.rar
(909.06 KB, 下载次数: 421)
源代码
https://pan.baidu.com/s/17KB1npgEoYy1a-BBQcHk4A
密码:t3xj
网盘源代码不需要sign和token值。
以下源码调用v2transapi,需要sign和token值,测试通过。。。
[C#] 纯文本查看 复制代码
#region 百度Baidu翻译v2transapi,需要sign和token值
//内容长度
public string Content_Length(string text, string fromlang, string tolang, string sign, string token)
{
string s = "query=" + HttpUtility.UrlEncode(text).Replace("+", "%20") + "&from=" + fromlang + "&to=" + tolang + "&simple_means_flag=3&sign=" + sign + "&token=" + token;
return s;
}
/// <summary>
/// 百度翻译
/// </summary>
/// <param name="text">待翻译文本</param>
/// <param name="fromLanguage">自动检测:auto</param>
/// <param name="toLanguage">中文:zh,英文:en</param>
/// <returns>翻译后文本</returns>
public string 百度Baidu(string text, string fromLanguage, string toLanguage)
{
CookieContainer cc = new CookieContainer();
string BaiduTransBaseUrl = "http://fanyi.baidu.com/";
BaiduHead(BaiduTransBaseUrl, cc);
var BaseResultHtml = GetBaiduBase(BaiduTransBaseUrl, cc);
//在返回的HTML中正则匹配gtk
Regex re = new Regex(@"(?<=window.gtk = ')(.*?)(?=';)");
var _gtk = re.Match(BaseResultHtml).ToString();
//由gtk值获取sign值
var sign = new get_tk().baidu_tk(text, _gtk);
//获取token值
Regex re2 = new Regex(@"(?<=token: ')(.*?)(?=',)");
var tok = re2.Match(BaseResultHtml).ToString();
string baiduTransUrl = "http://fanyi.baidu.com/v2transapi";
var ResultHtml = GetBaiduHtml(baiduTransUrl, cc, "", Content_Length(text, fromLanguage, toLanguage, sign, tok));
try
{
StringReader sr = new StringReader(ResultHtml);
JsonTextReader jsonReader = new JsonTextReader(sr);
JsonSerializer serializer = new JsonSerializer();
var r = serializer.Deserialize<TransResult>(jsonReader);
int len = r.trans_result.Data.Count;
string s = "";
for (int i = 0; i < len; i++)
{
if (i + 1 != len)
s += r.trans_result.Data[i].Dst + "\r\n";
else
s += r.trans_result.Data[i].Dst;
}
return s;
}
catch
{
return "服务器返回异常,请重试。";
}
}
public string BaiduHead(string url, CookieContainer cookie)
{
var html = "";
var webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = "HEAD";
webRequest.CookieContainer = cookie;
webRequest.Timeout = 20000;
webRequest.Accept = "*/*";
webRequest.Headers.Add("Accept-Encoding: gzip,deflate");
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)";
webRequest.GetResponse();
}
public string GetBaiduBase(string url, CookieContainer cookie)
{
var html = "";
var webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = "GET";
webRequest.CookieContainer = cookie;
webRequest.Timeout = 20000;
webRequest.Accept = "*/*";
webRequest.Headers.Add("Accept-Encoding: gzip,deflate");
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)";
try
{
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
Stream st = webResponse.GetResponseStream();
if (webResponse.ContentEncoding.ToLower().Contains("gzip"))
st = new GZipStream(st, CompressionMode.Decompress);
using (var reader = new StreamReader(st, Encoding.UTF8))
{
html = reader.ReadToEnd();
reader.Close();
webResponse.Close();
}
}
return html;
}
catch
{
return null;
}
}
public string GetBaiduHtml(string url, CookieContainer cookie, string refer, string content_length)
{
var html = "";
var webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.CookieContainer = cookie;
webRequest.Referer = refer;
webRequest.Timeout = 20000;
webRequest.Accept = "*/*";
webRequest.Headers.Add("Accept-Language: zh-CN;q=0.8,en-US;q=0.6,en;q=0.4");
webRequest.Headers.Add("Accept-Encoding: gzip,deflate");
webRequest.Headers.Add("Accept-Charset: utf-8");
webRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.ProtocolVersion = new Version(1, 1);
byte[] con_len_byte = Encoding.UTF8.GetBytes(content_length);
webRequest.ContentLength = con_len_byte.Length;
Stream requsetSteam = webRequest.GetRequestStream();
requsetSteam.Write(con_len_byte, 0, con_len_byte.Length);
requsetSteam.Close();
try
{
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
Stream st = webResponse.GetResponseStream();
if (webResponse.ContentEncoding.ToLower().Contains("gzip"))
st = new GZipStream(st, CompressionMode.Decompress);
using (var reader = new StreamReader(st, Encoding.UTF8))
{
html = reader.ReadToEnd();
reader.Close();
webResponse.Close();
}
}
return html;
}
catch
{
return null;
}
}
public class Data
{
public string Dst { get; set; }
public string Src { get; set; }
}
public class Trans_result
{
public string From { get; set; }
public string To { get; set; }
public List<Data> Data { get; set; }
}
public class TransResult
{
public Trans_result trans_result
{
get;
set;
}
}
#endregion
================================================腾讯翻译===========================================
腾讯翻译不支持 英文 到 日,韩,,,可以中文 到 日韩。
成品:
TencentTranslate.rar
(909.71 KB, 下载次数: 90)
源代码:
https://pan.baidu.com/s/17KB1npgEoYy1a-BBQcHk4A
密码:t3xj |