asp.net C # calls Baidu Pai to achieve online translation, English to Chinese _ practical skills

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
Then the code looks just like the red section:
Copy Code code as follows:

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 style\=" "Font\-size\: 18px\; Font\-weight\: bold\; "" \> (. +?) \<\/font\> "). GROUPS[1]. Value;
Enstr = Regex.Replace (Enstr, @ "\r|\n", "");
Enstr = rehtml (ENSTR);
Enstr = Regex.match (Enstr, @ "Double-click or drag/select \) (. +?) "). 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<jsonss> (Atitle);
title = Aa2.trans_result. (a). 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<jsonss> (a);
Val + + Aa.trans_result. (). 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 outstr = "";
if (!string. IsNullOrEmpty (str))
{
for (int i = 0; i < str. Length; i++)
{
Converts a Chinese character to a 10-in integer and then converts to a 16-Unicode Char
Outstr + = "\\u" + ((int) str[i]). ToString ("X");
}
}
return outstr;
}
Unicode characters converted to Chinese
public string UTC (String str)
{
String outstr = "";
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-integer, then converts to char asp.net
Outstr + = (char) int. Parse (Strlist[i], System.Globalization.NumberStyles.HexNumber);
}
}
catch (FormatException ex)
{
Outstr = ex. message;
}
}
return outstr;
}
}
}
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.