ASP. NET C # easy call Baidu online translator function

Source: Internet
Author: User

First go to Baidu: Register a Apikey

http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%99% Be%e5%ba%a6%e7%bf%bb%e8%af%91api

Get the code:

Add the following code:

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Web;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Web.Script.Serialization;

Using System.Text.RegularExpressions;

Namespace Fangyi

{

public partial class _default:system.web.ui.page

{

protected void Page_Load (object sender, EventArgs e)

{

Gsentities db = new gsentities ();

for (int i = 740; i < 900000; i++)

{

Try

{

System.Threading.Thread.Sleep (100);

GETURL ("http://www.52mvc.com/story/love/" + i + ". html", db); Acquisition

}

catch (Exception ex)

{

System.Threading.Thread.Sleep (1500);

}

}

Db. Dispose ();

}

private void GetUrl (string url, gsentities db)

{

Content cc = new content ();

String apikey = "Wqlofg9o2vs1lrix4mz3mdj8";

var enstr = gethtml.gethtml (URL, null);

var title = Regex.match (enstr. Replace ("\ n", ""), @ "(. +?) \<\/font\> "). GROUPS[1]. Value;

Enstr = Regex.Replace (Enstr, @ "\r|\n", "");

Enstr = rehtml (ENSTR);

Enstr = Regex.match (Enstr, @ "Double-click or drag \) (. +?) "). GROUPS[1]. Value;

var atitle = gethtml.gethtml ("http://openapi.baidu.com/public/2.0/bmt/translate?client_id="

+ Apikey + "&q=" + title + "&from=auto&to=auto", null);

JavaScriptSerializer jss2 = new JavaScriptSerializer ();

var Aa2 = Jss2. Deserialize (Atitle);

title = Aa2.trans_result. First (). DST;

Enstr = Enstr. Trim ();

String val = "\t\t";

if (enstr. Length < 2000)

{

int i = 0;

var list = Enstr. Replace ("", ""). Replace ("", "" "). Split ('. '). ToList ();

List. ForEach (c =

{

var a = gethtml.gethtml ("http://openapi.baidu.com/public/2.0/bmt/translate?client_id="

+ Apikey + "&q=" + C + "&from=auto&to=auto", null);

JavaScriptSerializer JSS = new JavaScriptSerializer ();

var AA = JSS. Deserialize (a);

Val + = Aa.trans_result. First (). DST + ". ";

++i;

if (i% 5 = = 0)

{

System.Threading.Thread.Sleep (15000);

Val + = "\n\t\t";

}

System.Threading.Thread.Sleep (1000);

});

Cc.title = title;

Cc.val = val;

Db.content.AddObject (CC);

Db. SaveChanges ();

}

}

public string rehtml (string HTML)

{

String pattern = @ "<[\s\S]*?>";

Regex regex = new regex (pattern);

HTML = Regex. Replace (HTML, "");

return HTML;

}

Convert Chinese to Unicode character forget never

public string CTU (String str)

{

String utstr = "";

if (!string. IsNullOrEmpty (str))

{

for (int i = 0; i < str. Length; i++)

{

Converts a Chinese character to a 10-based integer and then to a 16-Unicode character

Outstr + = "\\u" + ((int) str[i]). ToString ("X");

}

}

return outstr;

}

Unicode characters converted to Chinese

public string UTC (String str)

{

String utstr = "";

if (!string. IsNullOrEmpty (str))

{

string[] strlist = str. Replace ("\ \", ""). Split (' u ');

Try

{

for (int i = 1; i < strlist. Length; i++)

{

Converts a Unicode character to a 10-binary integer and then to a char Chinese character, ASP.

Outstr + = (char) int. Parse (Strlist[i], System.Globalization.NumberStyles.HexNumber);

}

}

catch (FormatException ex)

{

Utstr = ex. Message;

}

}

return outstr;

}

}

}

Of course, you can also add Google Translate and Microsoft online translator features in a similar way.

End

ASP. NET C # easy call Baidu online translator function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.