111,129
社区成员
发帖
与我相关
我的任务
分享main.Maxmum = (int)response.ContentLength; ;//为main窗体下载文件总长度赋值main.totalDownloadedByte += allmybyte;//为main窗体当前下载进度赋值
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace _KZone
{
public class loadLrc
{
public static WebClient client = new WebClient();
public static void gecixia(string name)
{
string geci_search_adress;
geci_search_adress = HttpUtility.UrlEncode(name, Encoding.Default);
//string ci = oAuthBase.UrlEncode(geci_search_adress);
string ci = geci_search_adress;
string geci = "http://mp3.sogou.com/gecisearch.so?query=" + ci + "&as=false&st=&ac=1&pf=&class=5&gecisearch.so=";
string ne = GetWebContent(geci);
Regex r2 = new Regex("((downlrc.jsp?).*(LRC歌词下载))", RegexOptions.IgnoreCase);
if (r2.IsMatch(ne))
{
string wangzhi = r2.Match(ne).Value;
string down_address = "http://mp3.sogou.com/" + wangzhi.Remove(wangzhi.IndexOf('"'));
DownloadFile(down_address, Application.StartupPath + @"\\Lrc\\" + name + ".lrc");
}
else
{
Regex r3 = new Regex("http://.*(?=.*lrc\")");
if (r3.IsMatch(ne))
{
string wangzhi1 = r3.Match(ne).Value;
string down_address1 = "http://mp3.sogou.com/" + wangzhi1.Remove(wangzhi1.IndexOf('"'));
client.Dispose();
DownloadFile( down_address1, Application.StartupPath + @"Lrc\\" + name + ".lrc");
}
else
{
Regex r4 = new Regex(@"lrc.aspxp.net/lrc.asp.* ");
if (r4.IsMatch(ne))
{
string wangzhi2 = r2.Match(ne).Value;
string down_address2 = wangzhi2.Remove(wangzhi2.IndexOf('"'));
DownloadFile("http://" + down_address2, Application.StartupPath + @"Lrc\\" + name + ".lrc");
}
}
}
}
public static string GetWebContent(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
main.Maxmum = (int)response.ContentLength; ;//为main窗体下载文件总长度赋值
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
catch
{
MessageBox.Show("出错");
}
return strResult;
}
public static void DownloadFile(string URLAddress, string fileName)
{
try
{
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[1000000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break;
startmbyte += m;
allmybyte -= m;
main.totalDownloadedByte += allmybyte;//为main窗体当前下载进度赋值
}
FileStream fstr = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, startmbyte);
str.Close();
fstr.Close();
client.Dispose();
}
catch
{
// MessageBox.Show(exp.Message, "Error");
}
}
}
}