asp.net implementation call Baidu Pai online translation English to Chinese

Source: Internet
Author: User
Tags sleep split thread

Register a Baidu 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:

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 = "tt";
if (enstr. Length <%)
{

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 + = "NTT";
}


System.Threading.Thread.Sleep (1000);
});
Cc.title = title;
Cc.val = val;
Db.content.AddObject (CC);
Db. SaveChanges ();
}
}
public string rehtml (string HTML)
{
String pattern = @ "<[sS]*?>";
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++)
{
///Convert the Unicode character to a 10-integer and then convert 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.