How to retrieve attachments from Gmail message Json [duplicate] - c#

How can I browse the email and download all attachments ?
public string Connect_Email ()
{
string Res = "";
try
{
mailclient = new TcpClient("pop.orange.fr", Convert.ToInt16("110"));
}
catch ( SocketException ExTrhown )
{
Res = "Unable to connect to server 1";
throw new Exception(ExTrhown.Message + "Unable to connect to server 1");
}
ns = mailclient.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
response = sr.ReadLine(); //Get opening POP3 banner
sw.WriteLine("USER " + "xxxxx#orange.fr"); //Send username
sw.Flush();
response = sr.ReadLine();
if ( response.Substring(0, 4) == "-ERR" )
{
Res = "Unable to log into server 2";
}
sw.WriteLine("PASS " + "xxxxx"); //Send password
sw.Flush();
response = sr.ReadLine();
if ( response.Substring(0, 3) == "-ER" )
{
Res = "Unable to log into server 3";
}
return Res;
}
public void Get_Attacht ()
{
string ClientName = "";
#region Chercher Attachment
sw.WriteLine("STAT"); //Send stat command to get number of messages
sw.Flush();
response = sr.ReadLine();
//find number of message
string[] nummess = response.Split(' ');
totmessages = Convert.ToInt16(nummess[1]);
//read emails
for ( int i = 1; i <= totmessages; i++ )
{
msg = null;
sw.WriteLine("top " + i + " 0"); //read header of each message
sw.Flush();
response = sr.ReadLine();
while ( true )
{
response = sr.ReadLine();
if ( response == "." )
break;
msg = msg + response + "\r\n";
}
//read attachment
attachment = null;
if ( Regex.Match(msg, "multipart/mixed").Success )
{
msg = null;
sw.WriteLine("retr " + i.ToString()); //Retrieve entire message
sw.Flush();
response = sr.ReadLine();
while ( true )
{
response = sr.ReadLine();
if ( response == "." )
break;
msg = msg + response + "\r\n";
}
int End = msg.IndexOf(".csv");
string LeFile = msg.Substring(End - 9, 9);
if ( Regex.Match(msg, LeFile + ".csv").Success )
{
data = msg.Split('\r');
startindex = 0;
index = 0;
lastindex = 0;
x = null;
ms = null;
fs = null;
while ( true )
{
attachment = null;
while ( !Regex.Match(data[index].Trim(), "filename").Success )
{
index++;
}
if ( index == data.Length - 1 ) break;
FileName_Email = data[index].Trim().Substring(42).Replace("\"", "");
//find start of attachment data
index++;
while ( data[index].Length != 1 )
{
index++;
}
if ( index == data.Length - 1 ) break;
startindex = index + 1;
//find end of data
index = startindex + 1;
while ( ( !Regex.Match(data[index].Trim(), "--0").Success ) && ( data[index].Length != 1 ) && ( index < data.Length - 1 ) )
{
index++;
}
if ( index == data.Length ) break;
lastindex = index - 2;
for ( int j = startindex; j <= lastindex; j++ )
{
attachment = attachment + data[j];
}
attachment = attachment + "\r\n";
if ( Regex.Match(FileName_Email.ToLower(), "csv").Success )
{
byte[] filebytes = Convert.FromBase64String(attachment);
FileStream LeFS = new FileStream(filePath + "\\testDEC.csv", FileMode.Create, FileAccess.Write, FileShare.None);
LeFS.Write(filebytes, 0, filebytes.Length);
LeFS.Close();
break;
}
}
}
}
}
sw.WriteLine("quit"); //quit
sw.Flush();
#endregion
}
It does not work, have you another simple idea ?

Try something like this
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("server");
pop3.UseBestLogin("user", "password");
foreach (string uid in pop3.GetAll())
{
IMail email = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine(email.Subject);
// save all attachments to disk
email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
}
pop3.Close();
}
// here is a reference link you can use as well
Getting Email Attachments

If you're trying to read e-mail via POP3, I would recommend using the OpenPOP.NET library instead of rolling your own. It's pretty easy to use.

Thanks you all for your contribution. Finally I use POP3:
public string Connect_Email()
{
string Res = "";
try
{
Pop3Client email = new Pop3Client("login", "password", "server");
email.OpenInbox();
while (email.NextEmail())
{
if (email.IsMultipart)
{
IEnumerator enumerator = email.MultipartEnumerator;
while (enumerator.MoveNext())
{
Pop3Component multipart = (Pop3Component)
enumerator.Current;
if (multipart.IsBody)
{
//Console.WriteLine("Multipart body:" + multipart.Name);
}
else
{
//Console.WriteLine("Attachment name=" + multipart.Name); // ... etc
byte[] filebytes = Convert.FromBase64String(multipart.Data);
//Search FileName
int Begin = multipart.ContentType.IndexOf("name=");
string leFileNale = multipart.ContentType.Substring(Begin + 5, 12);
FileStream LeFS = new FileStream(filePath + "\\" + leFileNale, FileMode.Create, FileAccess.Write, FileShare.None);
LeFS.Write(filebytes, 0, filebytes.Length);
LeFS.Close();
}
}
}
}
email.CloseConnection();
}
catch (Pop3LoginException)
{
Res = "Vous semblez avoir un problème de connexion!";
}
return Res;
}
It work well, but still I have to find and download the attachement my self.
byte[] filebytes = Convert.FromBase64String(multipart.Data);
//Search FileName
int Begin = multipart.ContentType.IndexOf("name=");
string leFileNale = multipart.ContentType.Substring(Begin + 5, 12);
FileStream LeFS = new FileStream(filePath + "\\" + leFileNale, FileMode.Create, FileAccess.Write, FileShare.None);
LeFS.Write(filebytes, 0, filebytes.Length);
LeFS.Close();

This not work more. Use Modern Authentication with Microsoft.Graph after create a cliente secret into AZURE and registered tha application them.

Related

How to communicate ( NETS Terminal Ingenico ) server using IP and port and get valid response

I have send a valid hex format data to ( NETS Terminal Ingenico ) using IP and port.
But I received a response (NACK - "\u0015" ),its should come response (ACK - "\u0006")
Sample request send : 0200184e455453303931353038313335353031301C033D
Sample text explain in below.
STX : 02 ,
COUNT :0018 ,
ECN : 4e4554533039313530383133 ,
FUCNTION CODE : 3535 ,
VERSION CODE :3031,
RFU : 30 ,
SEPARATOR : 1C,
ETX : 03 ,
LRC : 3D
I am not sure issue in code are data sending format.
try
{
if (txtECN.Text == "Auto" || txtECN.Text == "")
{
txtECN.Text = "NETS" + DateTime.Now.ToString("MMddhhmm");
}
//Message Header Header
var hex_txtECN = GetHex(txtECN.Text);
var hex_txtFuctionCode = GetHex(txtFuctionCode.Text);
var hex_txtVersionCode = GetHex(txtVersionCode.Text);
var hex_txtRFU = GetHex(txtRFU.Text);
var hex_txtSeparator = txtSeparator.Text;
var Req_Header = hex_txtECN + "|" + hex_txtFuctionCode + "|" + hex_txtVersionCode + "|" + hex_txtRFU + "|" + hex_txtSeparator;
var total_bytes = Req_Header.Replace("|", "");
var length = "00" + (total_bytes.Length) / 2;
var Message_Header = length + total_bytes;
//Message Header End
//Message Data start
var hex_txtTranTypeIndicator = GetHex(txtTranTypeIndicator.Text);
var hex_txtAmount = GetHex(txtAmount.Text);
var hex_txtCashAmount = GetHex(txtCashAmount.Text);
var hex_ECRReffNo = GetHex(ECRReffNo.Text);
var hex_txtSeparator2 = txtSeparator2.Text;
var Req_Data = hex_txtTranTypeIndicator + "|" + hex_txtAmount + "|" + hex_txtCashAmount + "|" + hex_ECRReffNo + "|" + hex_txtSeparator2;
var total_bytes1 = Req_Data.Replace("|", "");
var length1 = "00" + (total_bytes1.Length - 1);
var Message_Data = "";
if (txtTranTypeIndicator.Text != "")
{
var fieldCode1 = GetHex("T2");
var val1 = GetHex("02");
var len1 = "0001";
var separator1 = "1C";
var data1 = fieldCode1 + len1 + val1 + separator1;
var fieldCode2 = GetHex("T2");
var val2 = GetHex("000001");
var len2 = "0006";
var separator2 = "1C";
var data2 = fieldCode2 + len2 + val2 + separator2;
Message_Data = data1 + data2;
}
//Message Data send
var Message = Message_Header + Message_Data + txtETX.Text;
txtLRC.Text = LRC(Message).ToString();
var send_command = txtSTX.Text + Message + txtLRC.Text;
txtresponse.Text = txtSTX.Text + " " + length + Req_Header.Replace("|", " ") + " " + Req_Data.Replace("|", " ") + " " + txtETX.Text + " " + txtLRC.Text;
var endPoint = new IPEndPoint(IPAddress.Parse("192.168.0.250"), 3000);
using Socket client = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
await client.ConnectAsync(endPoint);
var messageBytes = Encoding.UTF32.GetBytes(send_command);
_ = await client.SendAsync(messageBytes, SocketFlags.None);
// Receive ack.
var buffer = new byte[1024];
var received = await client.ReceiveAsync(buffer, SocketFlags.None);
var response = char.ConvertFromUtf32(received);
var UTF8 = Encoding.UTF8.GetString(buffer, 0, received);
int sendTry = 0;
int ms = 0;
bool success = false;
while (true)
{
if (receivedACK)
{
success = true;
receivedACK = false;
receivedNACK = false;
break;
}
if (receivedNACK)
{
success = false;
receivedACK = false;
receivedNACK = false;
break;
}
if (ms > 1000)
{
ms = 0;
sendTry++;
if (sendTry == 3)
break;
_ = await client.SendAsync(messageBytes, SocketFlags.None);
// Receive ack.
buffer = new byte[1024];
received = await client.ReceiveAsync(buffer, SocketFlags.None);
response = char.ConvertFromUtf32(received);
UTF8 = Encoding.UTF8.GetString(buffer, 0, received);
if(response == ((char)0x06).ToString())
{
receivedACK = true;
}
else if (response == ((char)0x15).ToString())
{
receivedACK = false;
}
else
{
receivedACK = false;
}
}
System.Threading.Thread.Sleep(1);
ms++;
}
client.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
}

how to boost my read emails c# web app speed

I wrote c# code to read emails from my gmail account, using openpop.net library, and save them into a database. Everything is going right but it is so slow in loading the emails. I tried a parallel.for loop but it gave the error:
write method can not begin while other write method is running
Is there any way to use parallel processing in this case?
This is my read emails function:
public void Read_Emails()
{
Pop3Client pop3Client;
if (Session["Pop3Client"] == null)
{
pop3Client = new Pop3Client();
pop3Client.Connect("pop.gmail.com", 995, true);
pop3Client.Authenticate(usermail, pwd, AuthenticationMethod.UsernameAndPassword);
Session["Pop3Client"] = pop3Client;
}
else
{
pop3Client = (Pop3Client)Session["Pop3Client"];
}
int count = pop3Client.GetMessageCount();
for (int i = count; i >= 1; i--)
{
Message message = pop3Client.GetMessage(i);
FFromAdress = message.Headers.From.Address;
ddate = message.Headers.DateSent;
MMessageNO = i; //message.Headers.MessageId;
List<MessagePart> attachments = message.FindAllAttachments();
DataTable dt = get_Health_Institutions();
DataTable dt1 = get_INTERIOR_Institutions();
foreach (DataRow row in dt.Rows){health_result = FFromAdress.Equals(row["mail"]);}
foreach (DataRow row in dt1.Rows){interior_result = FFromAdress.Equals(row["mail"]);}
bool result = FFromAdress.Equals("sersersetrerttrtail.com");
if (health_result == true) //|| interior_result == true)
{ SSubject = message.Headers.Subject.Split(':')[1];
MessagePart body = message.FindFirstHtmlVersion();
body = message.FindFirstPlainTextVersion();
if (body != null)
{
string BBody1 = body.GetBodyAsText();
BBody = BBody1.Replace(' ', '\n');
}
if (attachments.Count == 0)
{
atta_count = 0;
AAtachments_Path = SSubject;
if (CHKsubjectexist_Health_Date(SSubject) == 0)
{ insertmail_health_date(FFromAdress, SSubject, ddate, BBody, AAtachments_Path, MMessageNO, atta_count); }
}
else
{ for (int m = 0; m < attachments.Count; m++)
{
atta_count = attachments.Count;
if (health_result == true & interior_result == false)
{ imagename = "healthRESULT_answer" + m+".jpg"; }
Foldername = SSubject.Trim();
AAtachments_Path = Foldername;
byte[] bdata = attachments[m].Body;
string folderpathh = "d:\\asdf\\" + Foldername;
System.IO.Directory.CreateDirectory(folderpathh);
string pathh = folderpathh + "\\" + imagename;
System.IO.File.WriteAllBytes(pathh, bdata);
////write image
String st = Server.MapPath(imagename);
FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);
fs.Write(bdata, 0, bdata.Length);
fs.Close();
}
if (CHKsubjectexist_Health_final(SSubject) == 0)
{ insertmail_Health_Final(FFromAdress, SSubject, ddate, BBody, AAtachments_Path, MMessageNO, atta_count); }
}
}
else if (interior_result == true)
{
SSubject = message.Headers.Subject.Split(':')[1];
MessagePart body = message.FindFirstHtmlVersion();
body = message.FindFirstPlainTextVersion();
if (body != null)
{
string BBody1 = body.GetBodyAsText();
BBody = BBody1.Replace(' ', '\n');
}
if (attachments.Count == 0)
{
atta_count = 0;
AAtachments_Path = SSubject;
}
else
{
for (int m = 0; m < attachments.Count; m++)
{
atta_count = attachments.Count;
imagename = "Interior_answer" + m+".jpg";
Foldername = SSubject.Trim();
AAtachments_Path = Foldername;
byte[] bdata = attachments[m].Body;
string folderpathh = "d:\\asdf\\" + Foldername;
// System.IO.Directory.CreateDirectory(folderpathh);
string pathh = folderpathh + "\\" + imagename;
System.IO.File.WriteAllBytes(pathh, bdata);
////write image
String st = Server.MapPath(imagename);
FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);
fs.Write(bdata, 0, bdata.Length);
fs.Close();
}
}
if (CHKsubjectexist_Interior(SSubject) == 0)
{ insertmail_interior(FFromAdress, SSubject, ddate, BBody, AAtachments_Path, MMessageNO, atta_count); }
}
else if (result == true)
{
SSubject = message.Headers.Subject;
if (CHKsubjectexist(SSubject) == 0)
{
MessagePart body = message.FindFirstHtmlVersion();
if (body != null)
{
BBody = body.GetBodyAsText();
}
else
{
body = message.FindFirstPlainTextVersion();
if (body != null)
{
BBody = body.GetBodyAsText();
}
}
if (attachments.Count == 0)
{
AAtachments_Path = " ";
}
else
{
foreach (MessagePart attachment in attachments)
{
atta_count = attachments.Count;
string filenameee = attachment.FileName;
imagename = filenameee.Split('/', '/')[3];
Foldername = SSubject;
AAtachments_Path = Foldername;
byte[] bdata = attachment.Body;
string folderpathh = "d:\\asdf\\" + Foldername;
System.IO.Directory.CreateDirectory(folderpathh);
string pathh = folderpathh + "\\" + imagename;
System.IO.File.WriteAllBytes(pathh, bdata);
////write image
String st = Server.MapPath(imagename);
FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);
fs.Write(bdata, 0, bdata.Length);
fs.Close();
}
}
if (CHKsubjectexist(SSubject) == 0)
{ insertmail(FFromAdress, SSubject, ddate, BBody, AAtachments_Path, MMessageNO, atta_count); }
}
}
}
}

Why when giving files names numbers to the names using a counter it's giving it strange numbers?

The first saved file name on the hard disk is: 201701311645---0 then 201701311645---1 then 201701311645---20 then 201701311645---21 then 201701311645---40 and 201701311645---41
But i want it to be saved as: 201701311645---0 then 201701311645---1 then 201701311645---2 then 201701311645---3 then 201701311645---4 and 201701311645---5
In the top i added a counter variable
private int countFilesNames = 0;
Then in a dowork event i also reset the counter to 0 once so if i start the backgroundworker over again it will start from 0.
private void bgwDownloader_DoWork(object sender, DoWorkEventArgs e)
{
Int32 fileNr = 0;
countFilesNames = 0;
if (this.SupportsProgress) { calculateFilesSize(); }
if (!Directory.Exists(this.LocalDirectory)) { Directory.CreateDirectory(this.LocalDirectory); }
while (fileNr < this.Files.Count && !bgwDownloader.CancellationPending)
{
m_fileNr = fileNr;
downloadFile(fileNr);
if (bgwDownloader.CancellationPending)
{
fireEventFromBgw(Event.DeletingFilesAfterCancel);
cleanUpFiles(this.DeleteCompletedFilesAfterCancel ? 0 : m_fileNr, this.DeleteCompletedFilesAfterCancel ? m_fileNr + 1 : 1);
}
else
{
fileNr += 1;
}
}
}
Then in the downloadFile method
private void downloadFile(Int32 fileNr)
{
FileStream writer = null;
m_currentFileSize = 0;
fireEventFromBgw(Event.FileDownloadAttempting);
FileInfo file = this.Files[fileNr];
Int64 size = 0;
Byte[] readBytes = new Byte[this.PackageSize];
Int32 currentPackageSize;
System.Diagnostics.Stopwatch speedTimer = new System.Diagnostics.Stopwatch();
Int32 readings = 0;
Exception exc = null;
try
{
writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
"---" + countFilesNames + ".png", System.IO.FileMode.Create);
}
catch(Exception err)
{
string ggg = err.ToString();
}
HttpWebRequest webReq;
HttpWebResponse webResp = null;
try
{
webReq = (HttpWebRequest)System.Net.WebRequest.Create(this.Files[fileNr].Path);
webResp = (HttpWebResponse)webReq.GetResponse();
size = webResp.ContentLength;
}
catch (Exception ex) { exc = ex; }
m_currentFileSize = size;
fireEventFromBgw(Event.FileDownloadStarted);
if (exc != null)
{
bgwDownloader.ReportProgress((Int32)InvokeType.FileDownloadFailedRaiser, exc);
}
else
{
m_currentFileProgress = 0;
while (m_currentFileProgress < size && !bgwDownloader.CancellationPending)
{
while (this.IsPaused) { System.Threading.Thread.Sleep(100); }
speedTimer.Start();
currentPackageSize = webResp.GetResponseStream().Read(readBytes, 0, this.PackageSize);
m_currentFileProgress += currentPackageSize;
m_totalProgress += currentPackageSize;
fireEventFromBgw(Event.ProgressChanged);
writer.Write(readBytes, 0, currentPackageSize);
readings += 1;
if (readings >= this.StopWatchCyclesAmount)
{
m_currentSpeed = (Int32)(this.PackageSize * StopWatchCyclesAmount * 1000 / (speedTimer.ElapsedMilliseconds + 1));
speedTimer.Reset();
readings = 0;
}
}
speedTimer.Stop();
writer.Close();
webResp.Close();
if (!bgwDownloader.CancellationPending) { fireEventFromBgw(Event.FileDownloadSucceeded); }
}
fireEventFromBgw(Event.FileDownloadStopped);
countFilesNames += 1;
}
I build the file name:
writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
"---" + countFilesNames + ".png", System.IO.FileMode.Create);
The move the counter forward by 1:
countFilesNames += 1;
But i'm getting other files names then i wanted.
Maybe there is a better way to give the files names some identity ? The problem is that if i will not give the files names some identity it will overwrite the files all the time. The files names are the same the content is not so i need to give each file another name.
Why don't you just increment the counter only when a file is written (since the variable doesn't look like it is accessed elsewhere) and not below:
writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
"---" + countFilesNames++ + ".png", System.IO.FileMode.Create);
This way the counter won't be incremented on errors.

Downloading file from redirecting URLs

I am trying to download mp3 from http://www.audiodump.com/. The site has a lot of redirections. However I managed getting a part of it working.
This is my method for getting all informations such as DL links, titles, mp3 durations.
private void _InetGetHTMLSearch(string sArtist)
{
if(_AudioDumpQuery == string.Empty)
{
//return string.Empty;
}
string[] sStringArray;
string sResearchURL = "http://www.audiodump.biz/music.html?" + _AudioDumpQuery + sArtist.Replace(" ", "+");
string aRet;
HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(sResearchURL);
webReq.Referer = "http://www.audiodump.com/";
try
{
webReq.CookieContainer = new CookieContainer();
webReq.Method = "GET";
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
aRet = reader.ReadToEnd();
//Console.WriteLine(aRet);
string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline);
if (aTable != null)
{
string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\"");
if (aInfos != null)
{
for(int i = 0; i < aInfos.Length; i++)
{
aInfos[i] = aInfos[i].Replace("\">", "*");
aInfos[i] = aInfos[i].Replace("</a> (", "*");
aInfos[i] = aInfos[i].Remove(aInfos[i].Length - 2);
sStringArray = aInfos[i].Split('*');
aLinks.Add(sStringArray[0]);
aTitles.Add(sStringArray[1]);
sStringArray[2] = sStringArray[2].Replace("`", "'");
sStringArray[2] = sStringArray[2].Replace("dont", "don't");
sStringArray[2] = sStringArray[2].Replace("lets", "let's");
sStringArray[2] = sStringArray[2].Replace("cant", "can't");
sStringArray[2] = sStringArray[2].Replace("shes", "she's");
sStringArray[2] = sStringArray[2].Replace("aint", "ain't");
sStringArray[2] = sStringArray[2].Replace("didnt", "didn't");
sStringArray[2] = sStringArray[2].Replace("im", "i'm");
sStringArray[2] = sStringArray[2].Replace("youre", "you're");
sStringArray[2] = sStringArray[2].Replace("ive", "i've");
sStringArray[2] = sStringArray[2].Replace("youll", "you'll");
sStringArray[2] = sStringArray[2].Replace("'", "'");
sStringArray[2] = sStringArray[2].Replace("'", "simplequotes");
sStringArray[2] = sStringArray[2].Replace("vk.com", "");
sStringArray[2] = _StringReplaceCyrillicChars(sStringArray[2]);
sStringArray[2] = Regex.Replace(sStringArray[2], #"<[^>]+>| ", "").Trim();
sStringArray[2] = Regex.Replace(sStringArray[2], #"\s{2,}", " ");
sStringArray[2] = sStringArray[2].TrimStart('\'');
sStringArray[2] = sStringArray[2].TrimStart('-');
sStringArray[2] = sStringArray[2].TrimEnd('-');
sStringArray[2] = sStringArray[2].Replace("- -", "-");
sStringArray[2] = sStringArray[2].Replace("http", "");
sStringArray[2] = sStringArray[2].Replace("www", "");
sStringArray[2] = sStringArray[2].Replace("mp3", "");
sStringArray[2] = sStringArray[2].Replace("simplequotes", "'");
aDurations.Add(sStringArray[2]);
}
}
else
{
//Console.WriteLine("Debug");
}
}
else
{
//Console.WriteLine("Debug 2");
}
//return aRet;
}
}
}
catch (Exception ex)
{
//return null;
////Console.WriteLine("Debug message: " + ex.Message);
}
}
I simply had to add referrer to prevent the search from redirection webReq.Referer = "http://www.audiodump.com/";
However when I want to download the mp3 I can't get it working. The urls are correct and checked with the ones I get when I download them manually rather than programmatically.
This is my mp3 download part:
private void _DoDownload(string dArtist, ref string dPath)
{
if (!Contain && skip <= 3 && !Downloading)
{
Random rnd = new Random();
int Link = rnd.Next(5);
_InetGetHTMLSearch(dArtist);
Console.WriteLine("--------------------------------> " + aLinks[0]);
string path = mp3Path + "\\" + dArtist + ".mp3";
if (DownloadOne(aLinks[Link], path, false))
{
hTimmer.Start();
Downloading = true;
}
}
else if (Downloading)
{
int actualBytes = strm.Read(barr, 0, arrSize);
fs.Write(barr, 0, actualBytes);
bytesCounter += actualBytes;
double percent = 0d;
if (fileLength > 0)
percent =
100.0d * bytesCounter /
(preloadedLength + fileLength);
label1.Text = Math.Round(percent).ToString() + "%";
if (Math.Round(percent) >= 100)
{
string path = mp3Path + "\\" + dArtist + ".mp3";
label1.Text = "";
dPath = path;
aLinks.Clear();
hTimmer.Stop();
hTimmer.Reset();
fs.Flush();
fs.Close();
lastArtistName = "N/A";
Downloading = false;
}
if (Math.Round(percent) <= 1)
{
if (hTimmer.ElapsedMilliseconds >= 3000)
{
string path = mp3Path + "\\" + dArtist + ".mp3";
hTimmer.Stop();
hTimmer.Reset();
fs.Flush();
fs.Close();
File.Delete(path);
Contain = false;
skip += 1;
Downloading = false;
}
}
}
}
private static string ConvertUrlToFileName(string url)
{
string[] terms = url.Split(
new string[] { ":", "//" },
StringSplitOptions.RemoveEmptyEntries);
string fname = terms[terms.Length - 1];
fname = fname.Replace('/', '.');
return fname;
} //ConvertUrlToFileName
private static long GetExistingFileLength(string filename)
{
if (!File.Exists(filename)) return 0;
FileInfo info = new FileInfo(filename);
return info.Length;
} //GetExistingFileLength
private static bool DownloadOne(string url, string existingFilename, bool quiet)
{
ServicePointManager.DefaultConnectionLimit = 20;
HttpWebRequest webRequest;
HttpWebResponse webResponse;
IWebProxy proxy = null; //SA???
//fmt = CreateFormat(
//"{0}: {1:#} of {2:#} ({3:g3}%)", "#");
try
{
fname = existingFilename;
if (fname == null)
fname = ConvertUrlToFileName(url);
if (File.Exists(existingFilename))
{
File.Delete(existingFilename);
}
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A";
webRequest.Referer = "http://www.audiodump.com/";
preloadedLength = GetExistingFileLength(fname);
if (preloadedLength > 0)
webRequest.AddRange((int)preloadedLength);
webRequest.Proxy = proxy; //SA??? or DefineProxy
webResponse = (HttpWebResponse)webRequest.GetResponse();
fs = new FileStream(fname, FileMode.Append, FileAccess.Write);
fileLength = webResponse.ContentLength;
strm = webResponse.GetResponseStream();
if (strm != null)
{
bytesCounter = preloadedLength;
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
//Console.WriteLine(
//"{0}: {1} '{2}'",
// url, e.GetType().FullName,
//e.Message);
return false;
}
//exception
} //DownloadOne
The method _DoDownload() is executed from a timer which runs every 250 milliseconds. This way works perfectly on other sites. However audiodump is giving me hard time with these redirections.
I am not a genius with httprequest. I managed solving the search issue however the download part is freaking me out. Any advice on how to manage the download issue?
You just need to set referrer to the page from where you got that download link. For example you grabbed links to files from page "http://www.audiodump.biz/music.html?q=whatever", then when downloading file set that as Referrer, not just "http://www.audiodump.biz".

Convert C# Console application application to Website/WebApp

I've made a console app (don't judge for it being badly written, I can't even do classes properly and I know that RegEx doesn't work with XML/HTML, but let's not mind that I'm not even a programmer)
int retryCount = 0;
int retryCount02 = 0;
int innerpercentage = 1;
HttpWebResponse response = null;
HttpWebResponse response02 = null;
//WebRequest groupingurl = WebRequest.Create("supersecretlink");
WebRequest groupingurl = WebRequest.Create("anothersupersecretlink"); // temp
var groupingurlread = (HttpWebResponse)groupingurl.GetResponse();
using (var groupingreader = new StreamReader(groupingurlread.GetResponseStream(), Encoding.ASCII))
{
string responseGrouping = groupingreader.ReadToEnd();
Console.WriteLine("--------");
var pattern = #"""ClassID"" Value=""(.*?)""";
var matches = Regex.Matches(responseGrouping, pattern)
.OfType<Match>()
.Select(m => m.Groups[1].Value)
.ToArray();
int percentage = 1;
int percentagedone = matches.Length;
foreach (string r in matches)
{
while (true)
{
try
{
var url = "themostseriouslinkever";
string req = File.ReadAllText(#"somanylinksrighthere", Encoding.Default);
var go = url + req;
go = String.Format(go, r);
WebRequest request = WebRequest.Create(go);
response = (HttpWebResponse)request.GetResponse();
break;
}
catch (WebException)
{
Console.WriteLine("Retry No. {0}", retryCount+1);
if (++retryCount < 3) continue;
throw;
}
}
retryCount = 0;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
string responseText = reader.ReadToEnd();
pattern = #"<PartNumber>(.*?)</PartNumber>";
matches = Regex.Matches(responseText, pattern)
.OfType<Match>()
.Select(m => m.Groups[1].Value)
.ToArray();
int innerpercentagedone = matches.Length;
foreach (string bbb in matches)
{
// -----
//Console.WriteLine(bbb);
while (true)
{
try
{
var url02 = "anothersecret";
string req02 = File.ReadAllText(#"seeecreet", Encoding.Default);
var go02 = url02 + req02;
go02 = string.Format(go02, bbb);
WebRequest request02 = WebRequest.Create(go02);
response02 = (HttpWebResponse)request02.GetResponse();
break;
}
catch (WebException)
{
Console.WriteLine("Retry No. {0}", retryCount02+1);
if (++retryCount02 < 3) continue;
throw;
}
}
retryCount02 = 0;
using (var reader02 = new StreamReader(response02.GetResponseStream(), Encoding.ASCII))
{
string responseText02 = reader02.ReadToEnd();
responseText02 = Regex.Replace(responseText02, (#"<\?xml.*\?>"), "");
//Console.WriteLine(responseText02);
//Console.WriteLine("</PartNumber>.*<");
responseText = Regex.Replace(responseText, ((#"<PartNumber>" + bbb + #"<\/PartNumber>")), responseText02);
//Console.WriteLine(responseText);
Console.Clear();
Console.WriteLine("(" + percentage + ") " + innerpercentage + "/" + innerpercentagedone + ". Total: " + percentage + " / " + percentagedone);
innerpercentage++;
}
}
//string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter outputFile = new StreamWriter(#"D:\Downloads\edasltxmlrequest\ConsoleApplication1\ConsoleApplication1\bin\Debug\test\" + r + ".xml"))
{
outputFile.WriteLine(responseText);
}
//Console.Clear();
//Console.WriteLine(percentage + " / " + percentagedone);
percentage++;
innerpercentage = 1;
//Console.WriteLine(responseText);
}
}
Console.WriteLine("--------");
Console.ReadLine();
}
}
}
So I wanted to ask if it's possible to make it run as a web app (http://www.amazingwebsite.com/ThisThingIWroteRightThere.php

Categories