复制代码 代码如下:
public static string GetGetRequest(string urlP,string encode){
if(null==urlP) return null;
string StrRetP = null;
Stream dataStream = null;
try{
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(urlP);
myHttpWebRequest.Timeout = 10000; // 10 secs
HttpWebResponse Objresponse =(HttpWebResponse)myHttpWebRequest.GetResponse();
//Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
if(Objresponse.StatusDescription == "OK"){//HttpStatusCode.OK
dataStream = Objresponse.GetResponseStream ();
Encoding objE = String.IsNullOrEmpty(encode)?Encoding.GetEncoding(0):Encoding.GetEncoding(encode);
StreamReader r = new StreamReader(dataStream,objE);
StrRetP= r.ReadToEnd();
}
}catch(Exception e){
StrRetP =e.Message;
}finally{
if(null!=dataStream) dataStream.Close();
}
return StrRetP;
}
复制代码 代码如下:
public static string GetMatchString(string text,string pattern,int point){
if(String.IsNullOrEmpty(text)||String.IsNullOrEmpty(pattern))return String.Empty;
Regex rx = new Regex(pattern,RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match match = rx.Match(text);
string word="";
if (match.Success) word = match.Groups[point].Value;
return word.Trim();
}
复制代码 代码如下:
public static void Main(string[] args){
string mess ="我们";
Console.WriteLine(HttpUtility.UrlEncode("我们"));
mess = GetGetRequest("http://translate.google.com/translate_t?langpair="+HttpUtility.UrlEncode("zh-CN|en")+ "&text="+HttpUtility.UrlEncode(mess,System.Text.UnicodeEncoding.GetEncoding( "Gb2312")),"utf-8");
//Console.WriteLine(mess);
mess = GetMatchString(mess,@"(<div id=result_box dir=""ltr"">)([?:/s/S]*?)(</div>)",2);
Console.WriteLine(mess);
}
新闻热点
疑难解答
图片精选